加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何确定rescue_from中哪个异常处理程序会在Ra

发布时间:2020-12-16 22:08:45 所属栏目:百科 来源:网络整理
导读:我有两个rescue_from处理程序,一个404处理程序,并捕获所有处理程序. catch总是被调用ActiveRecord :: RecordNotFound异常,并且404处理程序永远不会被调用.我的期望是具有更多特异性的处理程序将被调用,但这不会发生. application_controller.rb # ActiveReco
我有两个rescue_from处理程序,一个404处理程序,并捕获所有处理程序. catch总是被调用ActiveRecord :: RecordNotFound异常,并且404处理程序永远不会被调用.我的期望是具有更多特异性的处理程序将被调用,但这不会发生.

application_controller.rb

# ActiveRecord 404
rescue_from ActiveRecord::RecordNotFound do |e|
  ...
end

# Catch all unhandled exceptions
rescue_from Exception do |e|
  ...
end

api docs for rescue_from说:

Handlers are inherited. They are searched from right to left,from
bottom to top,and up the hierarchy. The handler of the first class
for which exception.is_a?(klass) holds true is the one invoked,if
any.

我解释了关于陈述错误.如何获得我正在寻找的行为?

解决方法

404处理程序永远不会被调用,因为所有的catch都总是在你的例子中被调用.问题在于处理程序定义的排序.它们从底部到顶部进行评估,意味着您最后定义的处理程序将具有最高优先级,并且您的第一个定义的处理程序将具有最低优先级.如果您切换订单,那么您将获得所需的行为.
# Catch all unhandled exceptions
rescue_from Exception do |e|
  ...
end

# ActiveRecord 404
rescue_from ActiveRecord::RecordNotFound do |e|
  ...
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读