Rack-timeout results in 500 instead of 503

697 Views Asked by At

I'm using heroku's rack-timeout gem, along with dynamic error pages as described here.

However, when timeout raises an exception, it get's routed as a 500 error rather than 503.

I could catch the exception with a rescue_from in my application controller and manually route to errors#503, but that would prevent plugins like Rollbar from recording the exception.

Is there a way to get the correct error page rendered and ensure plugins like Rollbar still get wind of the exception?

2

There are 2 best solutions below

0
On BEST ANSWER

I ended up using the rambulance gem, which provides a simple configuration option to solve this:

# config/initializers/rambulance.rb

Rambulance.setup do |config|
  config.rescue_responses = {
    "Rack::Timeout::RequestTimeoutException" => :service_unavailable
  }
end

The author has also written up some good reasons why not to use the approach I was previously using:

Remove custom errors page section from the guides

0
On

I know this is an old question, but there's no need to add a gem dependency for this.

rack-timeout raises an exception; the 500 results from that exception being unhandled. To handle that exception and get a 503 or whatever else you might want, add:

config.action_dispatch.rescue_responses["Rack::Timeout::RequestTimeoutException"] = :service_unavailable

to your application.rb file.