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

我有两个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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...