sinatra/sinatra — Issue #423: Exception handling blindly uses #code as HTTP status
Author: adiel-mittmann
Created: 2011-12-06 13:52:35+00:00
State: open

I found this problem when my Sinatra-based application wasn't sending any results to the browser and the log showed a 2002 HTTP status. This occurred because the MySQL daemon wasn't running and the data_objects gem threw an exception (of type DataObjects::SQLError) which is misunderstood by Sinatra as an object capable of informing an HTTP status code. This is the offending line, in base.rb:

````ruby
status boom.respond_to?(:code) ? Integer(boom.code) : 500
````

It turns out that DataObjects::SQLError *does* return a code, but as far as I can tell it's just an internal code. In my case, it's returning 2002, which is not a valid HTTP status code and causes Sinatra to misbehave from that point on.

Perhaps Sinatra could check for the code's validity and replace it with 500 if it's invalid, or, better yet, use a more specific method name, such as http_error_code for the method. If an exception object has a method with that name, it's safe to assume that it does indeed reply with a valid code, and not an internal one.