我的应用程序控制器中有一个 around_filter,用于将所有操作封装在超时块中,以便操作在达到 30 秒 Heroku 限制之前失败。我还有一个rescue_from Timeout::Error 来彻底挽救这些超时。不幸的是,rescue_from 仅在某些时候有效。
如果在控制器内执行时发生超时,它可以正常工作,但如果超时发生在视图或助手内,则无法救援。
Timeout::Error 继承自 Interrupt 和 SignalException,两者都无法正确救援。但是,拯救异常本身does在视图和助手中正确救援。
around_filter :timeout
rescue_from Timeout::Error, :with => :timeout_rescue
def timeout
Timeout::timeout(10){
yield
}
end
def timeout_rescue
# Rescued
end
还有其他方法可以挽救 Timeout::Error 使其正常工作吗?
我遇到了同样的问题。我正在使用 Rails 3 + Rack::Timeout 并尝试在 ApplicationController 中救援_from。
我最终使用了这个...
rescue_from Exception do |exception|
if exception.is_a?(Timeout::Error) || /execution expired/ =~ exception.message
# rescued from timeout error
end
raise
end
UPDATE
我修补了机架超时 gem 以正确返回 Timeout::Error。这是一个线程问题。官方宝石已更新:https://github.com/kch/rack-timeout https://github.com/kch/rack-timeout
新的首选方法如下。一般来说,从异常中拯救并不是一个好主意,如果可能的话应该避免。
class ApplicationController < ActionController::Base
rescue_from Timeout::Error, with: :handle_timeout
def handle_timeout(exception)
# handle timeout error
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)