Search code examples
http-status-codeshttpserver

Which status code to use?


I have script and it sometimes cause 500 Internal Server Error. I can handle that errors. But which status to use in answer when this occur?

Code example in perl:

# catch error
eval {
     here is code which sometimes cause 500
     ....
     $response_body =  handle_request();
}

# Check if error occur
if( $@ ) { 
     $response_body =  'Error was handled'; 
     # Should I change status from 500?
     # If I should change. Which status I should use?
     # 200 or something else?
}


return $response_body;    

Solution

  • What do you mean, you "handled" it? Did you completely recover and the client gets their requested data? Then it should be 200.

    Did you just log the exception and prevented the crash, but have no useful data to send to the client? 500 in this case.