Error Detection in Rails 3.2 with `config.exceptions_app = self.routes`

As stated in this post:

http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/

The newest error handling method is as follows:

# application.rb: config.exceptions_app = self.routes #routes.rb match "/404", to: "site#not_found" 

However, it does not take into account the fact that the error rails application also handles 500 errors, 422 errors (and, possibly, other errors directed to these two pages?)

So, I hacked a solution that looks like this:

 # routes.rb rack_error_handler = ActionDispatch::PublicExceptions.new('public/') match "/422" => rack_error_handler match "/500" => rack_error_handler 

It’s good that he holds my 500 pages lightweight.

Are there any other bugs I have to catch? My understanding is that although two rack applications will be used on a 500-page page, it is still safely isolated from the main Rails application. Is it strong?

Thanks!

+7
source share
2 answers

I add rescue from the application controller

  if Rails.env.production? 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 rescue_from ActionView::MissingTemplate, :with => :render_not_found end def render_not_found(exception) logger.info("render_not_found: #{exception.inspect}") redirect_to root_path, :notice => 'The page was not found.' end 

and then add error_controller to save route errors by adding this to the bottom of my routes file.

  match "*path", :to => "errors#routing_error" 
+1
source

try it

Update config/application.rb

 config.exceptions_app = self.routes 

and route file

 match "/404", :to => "errors#not_found" 
+1
source

All Articles