I have to design a DAO that makes a call to a REST WS.
This WS must return the user Credentials from the given username and password.
Case 1 : the user was found => the REST WS sends http code 200 and a credentials response.
Case 2 : the user was not found => the REST WS sends http code 400 and an error object with the cause.
Case 3 : the user was found but his account is disabled => the REST WS sends http code 400 and and error object with the cause.
Case 4 : the REST WS is not available
What is the best way in my DAO to map the REST WS response ?
1 – I throw funtionnal checked exceptions in my DAO to treat the error objects cases and i return credentials response objects in normal cases. When REST WS is unavailable, i throw an unchecked exception
2 – I don’t throw any functionnal exception in my DAO, as it is the Service layer’s job. I return what the REST WS is returning, ie credentials responses and error responses in a wrapped object for example and i let the Service layer check for these objects to do the right job. When REST WS is unavailable, i throw an unchecked exception
3 – I only throw unchecked exceptions for error cases and i let the service layer decide what to do with it. And i return only Credentials response.
Thank you very much in advance.
I prefer option 1 because your DAO is taking responsibility for understanding what the remote datasource is returning. Your service layer sits above your DAO and should not have to understand any intricacies of the remote source; this includes how errors are returned over the wire.