Question

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?

Was it helpful?

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.

OTHER TIPS

It depends on why the exception is thrown since they can be used for almost any error. If it's thrown because some id in the URI is not found in the database I'd say 404. On the other hand if it's because the database is down I would throw a 500. If an exception is thrown but the resulting page would still be useful to the user I would say return 200.

Review the Status Code Definitions. 500 or 400 should do for general issues, however, the more detailed you can be then the more useful the returned status will be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top