I would say this heavily depends on what kind of API you're providing.
I were to always include a field called ack
or something similar in every response that has three states: failure
, warning
, success
. Success obviously being everything went well. On warning, the request went through and the JSON will contain the expected output, but it will also include a warning
string, or even better in case multiple warnings could occur an array called errors
which consists of multiple objects containg code
, string
and type
. This array will also be returned in case of failure
, and nothing else but this array.
The array contains one object per error or warning, having a code (I would suggest going with your initial idea of 10001, 10002, ...) and a string explaining the error in a very short phrase (e.g. Username contains invalid characters
). The type
is either error
or warning
, which is useful in case of a failure
ack that contains not only errors but also warnings.
This makes it easy to look up errors by their code (I would provide a page, also with an API, that contains all the error codes in a table along with their short and long description plus common causes/fixes/etc. - All this information should also be available via an API where they can be accessed by providing the error code) while still having a quick short text response so the user can tell what's wrong in most cases without having to look up the error.
This also allows for easy output of warnings and errors to the end user, not just the developers. Using my idea with the API call to get informations about an error, developers using your API could easily provide full information about errors to end-users when needed (including causes/fixes/whatever you see fit).