Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 Error Catching

I think Rails 3.1 is changing the way that errors are raised. Can anyone assist or confirm this? I'm attempting to create custom errors pages with Rails 3.1.0.rc1

unless 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
    rescue_from ActionController::UnknownAction, :with => :render_not_found
end

^^ This doesn't work.

config.consider_all_requests_local       = true

That is in my development environment by default. I'm assuming Rails 3.1 removes the "action_controller" but I can't confirm this anywhere.

Thanks!

like image 750
Robert Ross Avatar asked May 25 '11 01:05

Robert Ross


2 Answers

I'm assuming the following code appears in your ApplicationController?

unless 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
  rescue_from ActionController::UnknownAction, :with => :render_not_found
end

If so, try replacing this line:

unless config.consider_all_requests_local

with this line (pre Rails 3 I think):

unless ActionController::Base.consider_all_requests_local

or this (post Rails 3):

unless Rails.application.config.consider_all_requests_local
like image 153
Matt Huggins Avatar answered Oct 11 '22 21:10

Matt Huggins


I don't believe Matt's solution will catch routing errors in Rails 3.0/3.1.

Try putting the following in your application.rb:

# 404 catch all route
config.after_initialize do |app|
  app.routes.append{ match '*a', :to => 'application#render_not_found' } unless config.consider_all_requests_local
end

See: https://github.com/rails/rails/issues/671#issuecomment-1780159

Worked well for me!

like image 45
dleavitt Avatar answered Oct 11 '22 22:10

dleavitt