Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API error code 500 handling

Tags:

We are building a new REST API.

I was arguing that error code 500 (Internal Server Error) should never be returned.

Now, of course if you know the client's params are wrong or something you have everything under control and can return some appropriate error code (e.g. 422).

So if an unexpected error occurs the server could:

  1. NOT catch unexpected errors so that 500 bubbles up to the client
  2. Catch any unexpected errors and return some error code signaling an "unexpected situation" (honestly I couldn't find any such error code!)

Are there other options?

like image 581
transient_loop Avatar asked Jan 07 '15 18:01

transient_loop


People also ask

Should API ever return 500?

It is completely acceptable, however, to catch any out of process errors and interpret them as a 5xx response, but be aware that you should also include further information in the response to indicate exactly what failed; and even better if you can include SLA times to address.

How do you handle errors in API?

The simplest way we handle errors is to respond with an appropriate status code. Here are some common response codes: 400 Bad Request – client sent an invalid request, such as lacking required request body or parameter. 401 Unauthorized – client failed to authenticate with the server.

How do I send HTTP error messages 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.

Can T currently handle this request HTTP error 500?

The HTTP 500 message states that the server can't handle the request because of an unexpected condition. The problem may lie in browser cache, third-party themes and plugins, or PHP memory limit. It could also be due to a broken . htaccess file.


2 Answers

It is a server error, not a client error. If server errors weren't to be returned to the client, there wouldn't have been created an entire status code class for them (i.e. 5xx).

You can't hide the fact that you either made a programming error or some service you rely on is unavailable, and that certainly isn't the client's fault. Returning any other range of code in those cases than the 5xx series would make no sense.

RFC 7231 mentions in section 6.6. Server Error 5xx:

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method.

This is exactly the case. There's nothing "internal" about the code "500 Internal Server Error" in the sense that it shouldn't be exposed to the client.

like image 86
CodeCaster Avatar answered Oct 23 '22 21:10

CodeCaster


The real question is why does it generate a 500 error. If it is related to any input parameters, then I would argue that it should be handled internally and returned as a 400 series error. Generally a 400, 404 or 406 would be appropriate to reflect bad input since the general convention is that a RESTful resource is uniquely identified by the URL and a URL that cannot generate a valid response is a bad request (400) or similar.

If the error is caused by anything other than the inputs explicitly or implicitly supplied by the request, then I would say a 500 error is likely appropriate. So a failed database connection or other unpredictable error is accurately represented by a 500 series error.

like image 45
tokkov Avatar answered Oct 23 '22 21:10

tokkov