Ash
Ash

Reputation: 25652

rescue_from Error Handler In Rails Not Catching All Exceptions

I have the custom error handling code in my ApplicationController class:

  unless ActionController::Base.config.consider_all_requests_local
    rescue_from Exception, :with => :render_error
    rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
    rescue_from ActionController::RoutingError, :with => :render_not_found
    rescue_from ActionController::UnknownController, :with => :render_not_found
  end

However, If I type a mashed and invalid URL like: '/we/efe/qwe/wqeqw/qwe' The system with config.consider_all_requests_local = false Gives me the standard Rails 404 page instead of being caught by:

rescue_from ActionController::RoutingError, :with => :render_not_found

Looking at the Exception Trace Log, shows that Rails is raising a ActionController::RoutingError (No route matches [GET] "/we/efe/qwe/wqeqw/qwe"): however, I cannot determine why its not being caught by my handler.

Why would this not be working?

Upvotes: 2

Views: 1310

Answers (1)

Ash
Ash

Reputation: 25652

Apparently its the way that Rails 3.2+ raises ActionController::RoutingError exceptions.

The answer is that you need to add a default named route to the bottom of your routes to catch all requests that do not get routed:

get '*unmatched_route', :to => 'application#no_route'

Upvotes: 3

Related Questions