使用`config.exceptions_app = self.routes`修复Rails 3.2中的错误 [英] Rescuing errors in Rails 3.2 with `config.exceptions_app = self.routes`

查看:90
本文介绍了使用`config.exceptions_app = self.routes`修复Rails 3.2中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此帖子:

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

处理错误的最新方法如​​下:

The newest way to handle errors looks like this:

# application.rb:
config.exceptions_app = self.routes

#routes.rb
match "/404", to: "site#not_found"

但是,他没有解决Rails错误应用程序还处理500个错误,422个错误(可能还有其他错误)的事实错误集中到这两个页面上?)

However, he doesn't address the fact that the rails error app also handles 500 errors, 422 errors (and possibly other errors funneled to those two pages?)

所以我整理了一个看起来像这样的解决方案:

So I've hacked together 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

这很好,它可以使我的500页保持轻巧。

It's good in that it keeps my 500 pages lightweight.

我还应该捕捉其他错误吗?
我的理解是,尽管500页现在将使用两个机架应用程序,但仍可以与主Rails应用程序安全地隔离开来。

Are there other errors I should be catching as well? My understanding is that although the 500 page will now be using two rack apps, it is still safely isolated from the main Rails App enough. Is this strong?

谢谢!

推荐答案

我添加了救援应用程序控制器中的froms

I add the rescue froms in 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

,然后添加errors_controller来挽救路线错误将其添加到路线文件的底部

and then add a errors_controller to rescue the route errors adding this to the bottom of my routes file

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

这篇关于使用`config.exceptions_app = self.routes`修复Rails 3.2中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆