REST API Error handling
In this article we saw, in general, which HTTP methods and status code to use in CRUD operation.
Now let’s see better which http status code to use in our API.
There are more than 70 status code, but I suggest to use only the common ones.
HTTP Status code | When to use |
200 OK | all GET, PUT, DELETE |
201 Created | after insert (POST) |
400 Bad Request | Client wrong params |
404 Not Found | during a GET and the object is not found |
401 Not Authorized | |
403 Forbidden | |
500 Internal Server Error | in every back end issue |
Standard Error Template
{ text: "message", timestamp: new Date(), method: httpMethod, endpoit: endpointInformation, errors: [ { code: codeSpecificApplication, text: "errorMessage", hints: ["hints to help the user", "hint 2", ...], info: "Link to more info" } ], payload: {request payload} (optional, just for debug) }