I am currently writing an API / some clients for chess games.
The developers should access the API via one script (xhrframework.php) and submit the actions via GET. It is possible that they make some errors when they submit the actions (No PHPSESSID sent, no valid PHPSESSID, move was not valid, it's not their turn, ...).
So I thought about the posibilities how to display errors. I came up with some ideas how to inform the programmer, that he made an error:
I thoguht the third solution might be a good idea as the xhrframework.php should only be accessed by a programmer who quite possible took already a look at the API documentation.
Now I would like to know if a standard for Web-API-Error messages does exist. How did others (e.g. Google Maps API) solve this? Should I simply output a blank page with the content "ERROR: 004" or without padding "ERROR: 4"?
Which errors should get which numbers? Does ist make sense to group errors by their numbers, e.g. all errors beginning with 1 were authentification errors, all errors with two game logic errors? Would it be better to begin with error 1 and use each number?
Google Maps API
If I make a wrong call to the Google Maps JS-API, it returns Java-Script with a message in clear German (as I live in Germany, I guess).
The Google Static Maps API returns a message as text in clear English if I make a wrong call.
Most API services follow the HTTP error code system RFC2817 with ranges of error codes for different types of error:
1xx: Informational - Request received, continuing process
2xx: Success - The action was successfully received, understood, and accepted
3xx: Redirection - Further action must be taken in order to complete the request
4xx: Client Error - The request contains bad syntax or cannot be fulfilled
5xx: Server Error - The server failed to fulfil an apparently valid request
In an API context you would mostly use the 4xx values to denote error conditions to do with the validation of requests. 49x are generally used for security error conditions.