I'm writing an application based on a RESTful API located in a server (written in Python). An Android app will access the API endpoints to get, post, put or delete data. The usual.
The server has a getUser endpoint:
<app base URL>/users/<user email>
If no user has that e-mail, then a response with http error code 404 is returned.
The client API offers the rest of the application a method with the following signature:
User getUser(String email)
The problem Java gives me here is, I can only return 2 distinct values: a User object and null. However, null isn't expressive enough to distinguish between 'no internet connection' or 'there's no user with that e-mail'.
How should I communicate to the calling method that no user exists with that e-mail?
- Should I throw an exception, f. ex. "NoAssetFound"?
- Should I keep a "last error occurred" mailbox such as in C, which the user can check after calling getUser() in order to know what happened?
- Should I wrap the User object in another object which contains error information?
What are some common patterns to solve this problem?