Error handling is all about dealing with responses from the API when something goes wrong.
Each error response from the API contains a HTTP status code and a message that describes what went wrong.
RESPONSE CODES
There are 5 HTTP statuses returned from the API:
- HTTP status code: 200 – OK. Every thing is OK
- HTTP status code: 401 – Unauthorized. OAuth 2 credentials are not valid
- HTTP status code: 403 – Forbidden. You are not allowed access the resource you are trying to access
- HTTP status code: 404 – Not found. The resource you are looking for cannot be found
- HTTP status code: 500 – Internal server error. We messed up! The request you made could not be serviced because of something we did wrong on the server
The first of the responses above (HTTP status code: 200) does not need to be handled as it is not an error. However, if you get any of the other response codes (HTTP status codes: 401, 403, 404 and 500) it means that something went wrong and you may want to handle the error response it in your code.
An error response is sent with the HTTP status code in the header of the response and the body contains an error message which gives some details about what went wrong and why.
JSON
The error message that is sent in the body of the response is structured as a JSON object and will look something like:
{“errorMessage”:”Oops you are not permitted to view this object (12345) because it belongs to someone else.”}