Handling HTTP status codes on the client side
We spent quite some time addressing how RESTful services should represent each state, including erroneous ones, gracefully. A well-defined API should demand from its consumers to handle all its errors gracefully and to provide as much information per state as required, rather than just stating "An error has occurred". That is why it should look up the returned status code and clearly distinguish between client requests such as 400 Bad Request
or 415 Unsupported media types
caused by faulty payload, caused by wrong media types, or authentication-related errors, such as 401 Unauthorized
.
The status code of an erroneous response is available in the error
callback of the jQuery callback function and should be used to provide detailed information back to the request:
$.ajax({ url: "http://localhost:3000/catalog/v2/", type: "POST", dataType: "json", data: JSON.stringify(newItem), success: function (item, status...