Exceptions Everywhere

Most of the APIs have a defined format in which they return errors. This contract helps the API consumers in handling the errors.

At m.Paani, we too have a contract for raising errors. The API sends a JSON list containing N error objects. A typical error response would look something like

Format of a typical error response

field: Contains name of the field only if it’s a field error else returns non_field_errors

code: Internal Error Codes (as most non trivial systems have a lot of domain specific errors and edge cases). This also provides granularity and makes debugging a bit easier.

Consider this - API returns an HTTP 401 UNAUTHORIZED, it’s impossible to identify the actual cause. This could have been raised because token was invalid or expired or perhaps it wasn’t sent in the first place.

client_message: A user friendly message that can be directly shown to the user.

server_message: A message containing more technical information that can help in debugging the issue.