REST API JSON error code and error message
api, json, rest, standards
Solution
Nobody can stop API designers to return multiple possible error responses. Decision is up to them only but in my opinion you should return most relevant error occurred from server side.
There can be many reasons behind it like below :
- Unified Error Handling by all clients.
- Most relevant error caused will help client more actually as he will not have to take care about all probabilistic error caused.
- JSON structure simplicity. ( Error Array will be avoided. )
Problem
I have been writing Web Services from a recent past, this is a sample success and error response. error ``` { "code": 1150, "status": false, "message": "API Student does not exist.", "serverTime": "2013-11-29 09:47:52" } ``` success ``` { "code": 200, "status": true, "data": { "id": 49 }, "serverTime": "2014-04-17 05:06:17" } ``` With regards to returning errors I have a confusion, why do we always return one error code and one message, for example, when username and password is required as input params, say a blank request is made, so what I return is a error code 1100 and error message "Username is incorrect". I never return the whole list of errors, for example, in this case, two error message with two error codes should be sent so it saves end users data & time. This is a sample of what I suggest? ``` { "code": 1010, "status": false, "errors": [ { "code": 1000, "name": "Username invalid" }, { "code": 1001, "name": "Password invalid" }, { "code": 1002, "name": "Password not strong" } ], "serverTime": "2013-12-03 12:34:02" } ``` Why is this not a good way to do? I have not seen this in either Twitter API or Facebook API.