ruby on rails 3.1 global exception handler

你。 提交于 2019-12-13 19:16:13

问题


I'm developing an app with Rails 3.1.2 but I can't find some documentation that works with errors / exception (like 404) on this version of rails.

i have tried things like:

In application controller

rescue_from ActiveRecord::RecordNotFound,ActionController::RoutingError, 
             ActionController::UnknownController, ActionController::UnknownAction, :NoMethodError, :with => :handle_exception 

  def handle_exception 
   render :template => 'error_pages/error'
  end 

environment/development.rb

config.consider_all_requests_local = false

Where can I find a solution?

Thanks in advance...


回答1:


This should work:

In application controller

  class NotFound < StandardError; end
  rescue_from NotFound, :with => :handle_exception

  def handle_exception 
   render :template => 'error_pages/error'
  end



回答2:


Look at action_dispatch/middleware/show_exceptions.

From the documentation in the source:

# This middleware rescues any exception returned by the application
# and wraps them in a format for the end user.

Short story short, it renders ActionDispatch::ShowExceptions.render_exception when the wrapped application (Rails, in your case), encounters an unrescued exception.

If you look through the default implementation, it ends up rendering something like public/500.html, which is what you see in the production environment. Overwrite the method or method chain it as you see fit to add your own implementation.



来源:https://stackoverflow.com/questions/8495151/ruby-on-rails-3-1-global-exception-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!