Search code examples
httphttp-status

What is the correct HTTP status for exceptions?


What HTTP status should I return if my script throws an exception?

200 OK

or

500 Internal Server Error

Let's say user request parameters are correct but there is a bug in my script which causes an error message to appear instead of a proper response (XML, JSON or other format). What should be the HTTP status?


Solution

  • 500 Internal Server Error is the correct status if the error can't be fixed by the client changing their request.

    Use any of the 4XX statuses if the client might be able to fix their request to avoid the error (or 404 if the resource wasn't found).

    200 OK is not the appropriate status in almost any error situation, because then the client thinks things are running normally (which they are not) and may continue to make the same error-causing requests.

    Familiarize yourself with the available status codes in RFC2616 and find one that most appropriately fits the situation.