So, being a developer I have a very basic question, in a REST standard we have 100's of error code for specific reason like:
- 4xx if resource related
- 5xx if exception occurred in server
and many more.
Now when it comes to the implementation there are situations when we return 404 directly as the response status code with the error message in the response body. With this approach there is a thing that I think a bit confusing, what if the URI itself is never being made, that means suppose /a/b is not implemented and being any server they respond 404, and as a client they check the code and say that the user is not found if they are searching for the user with this API.
Instead what I feel (correct me if I am wrong) is that if the call is successfully completed in the server (without any exceptions and errors) we return 200 and in the response body return in a specific format like:
{
"status" : boolean, // if the overall call succeeded
"message" : string, // message from server
"code" : integer, // code, http code or business level code
"data" : object,//actual data
"type" : string, // type of the data like object, basic, array, (basically a value from enum)
}
The response code of any call will always be 200, with the specific code being available in code key of the response format.
Now coming to the usage of these REST call from client prospective, in the client whether it is browser, IOS, Android or Desktop app, we call the API and check for 200 as the Response code and all our further functionality will be dependent on status & code key of the response body itself. Again if the Response Code itself is not 200 then that it actually the problem with respect to the server.
Coming to the SDK implementations of the API's, we can do the same inside of them, by always checking the status and code if Response Code is 200, and rejecting directly non 200 Response Codes.
I feel with this approach the client side as well as SDK side the implementation would be more generic and straight forward.
Please correct me if I am wrong? Please shed some views.
Thanks in advance.