Check the order of your rescue_from handlers!

Our rescue_from handlers used to be defined like shown below. This might look okay to you. At first glance everything looks fine, right?

class WidgetsController < ActionController::Base
  rescue_from ActionController::RoutingError, :with => :render_404
  rescue_from Exception,                      :with => :render_500
end

Turns out it’s not okay at all. Handlers are searched from bottom to top. This means that they should always be defined in order of most generic to most specific. Or in other words, the above code is exactly the wrong thing to do. Instead, we need to write our handlers like shown here.

class WidgetsController < ActionController::Base
  rescue_from Exception,                      :with => :render_500
  rescue_from ActionController::RoutingError, :with => :render_404
end