【问题标题】:rescue_from Error Handler In Rails Not Catching All ExceptionsRails 中的rescue_from 错误处理程序未捕获所有异常
【发布时间】:2014-09-01 22:40:54
【问题描述】:

我的 ApplicationController 类中有自定义错误处理代码:

  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

但是,如果我输入一个混搭且无效的 URL,例如:'/we/efe/qwe/wqeqw/qwe' 带有config.consider_all_requests_local = false 的系统会给我标准的 Rails 404 页面而不是被捕获:

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

查看异常跟踪日志,显示 Rails 正在引发 ActionController::RoutingError (No route matches [GET] "/we/efe/qwe/wqeqw/qwe"):,但我无法确定为什么它没有被我的处理程序捕获。

为什么这不起作用?

【问题讨论】:

  • 能否请您发布render_not_found 方法。并从公共文件夹中删除原来的 404.html 和 500.html。
  • 这是开发还是测试还是生产环境?
  • 不需要 - 显然它是 Rails 3.2+ 引发 ActionController::RoutingError 的方式 - 答案是您需要在路由底部添加一个默认命名路由(捕获所有请求未路由:get '*unmatched_route', :to => 'application#no_route'.)

标签: ruby-on-rails exception-handling


【解决方案1】:

显然它是 Rails 3.2+ 引发 ActionController::RoutingError 异常的方式。

答案是你需要在你的路由底部添加一个默认命名路由来捕获所有没有被路由的请求:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2016-12-08
    相关资源
    最近更新 更多