Does a standard for errors / error codes exist?

Martin Thoma picture Martin Thoma · Nov 9, 2011 · Viewed 10.6k times · Source

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:

  1. via an error message in clear english
    • +: It is clear what the error should mean
    • +: Information how to fix this error can be added
    • -: The message might change as my english isn't very good
    • -: The length of the message differs quite a lot - this might be important for C programmers
  2. via ab error message constant in english - something like WRONG_PHPSESSID, MISSING_PHPSESSID, INVALID_MOVE, NOT_YOUR_TURN, ...
    • +: It's understandable for a human
    • 0: It's almost sure that the message doesn't change
    • -: The length of the message might differ a bit
  3. via an error code with one table in the documentation where the programmer can find the meaning of the error code
    • +: The error code would be constant
    • +: The lenght of every error code could be the same
    • -: It's cryptic

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.

Answer

Stuart picture Stuart · May 15, 2013

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.