Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to convey error messages during calls to a REST service?

Tags:

rest

I'm writing a REST based web service, and I'm trying to figure out the best way to handle error conditions.

Currently the service is returning HTTP Errors, such as Bad Request, but how can I return extra information to give developers using the web service an idea what they're doing wrong?

For example: creating a user with a null username returns an error of Bad Request. How can I add that the error was caused by a null username parameter?

like image 313
Alan Avatar asked Jul 23 '10 05:07

Alan


People also ask

How do I send a proper error in REST API?

The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the error message in the annotation's reason field. Although we can only return a generic error message, we can specify exception-specific error messages.

What is an API error message?

Generally speaking, it means one of two things — something was so wrong in your request or your handling that the API simply couldn't parse the passed data, or the API itself has so many problems that even the most well-formed request is going to fail.


1 Answers

According to the HTTP spec, the text that comes after the three digit response code, the "Reason-Phrase", can only be replaced with a logical equivalent. So you can't respond with 400 null user and expect anything useful to happen. Indeed, The client is not required to examine or display the Reason- Phrase.

In general, the HTTP response entity (typically the page that accompanies the response) should contain information useful to the client to guide them forward, even when the response is an error. On the web, most such errors are HTML, and are devoid of machine readable information, but most browsers do show the error to the user (and SO's error page is pretty good!).

So for a primarily machine readable resource you have two options:

  1. Pass a human readable message anyway. Return 400 Bad Request with a HTML response, which the client may opt to show to the user. It's dead easy but it's a bit like throwing an unchecked exception, it passes all the hard work to the client, or indeed the end user.
  2. Allow clients to recover. Return 400 Bad Request with a machine readable response which is part of your API, so clients can recover from known error conditions. This is harder, like throwing a checked exception, it becomes part of the API, and it allows clients to recover gracefully if they want to.

You could even make the server support both scenarios by defining a media type for the machie readable error recovery document, and allow clients to "accept" them: Accept: application/atom+xml, application/my.proprietary.errors+json

Clients that forget the mandatory field can opt in to getting machine readable errors or human readable errors by choosing to Accepting the error media type.

like image 177
mogsie Avatar answered Jan 03 '23 16:01

mogsie