Rescue_from不能从视图或助手中救出Timeout :: Error [英] Rescue_from doesn't rescue Timeout::Error from views or helpers

查看:115
本文介绍了Rescue_from不能从视图或助手中救出Timeout :: Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序控制器中有一个around_filter,用于将所有动作包含在一个超时块​​中,以使动作在达到30秒Heroku限制之前失败.我也有一个rescue_from Timeout :: Error来彻底拯救这些超时.不幸的是,result_from仅在某些时间有效.

如果在控制器内执行时发生超时,则工作正常,但如果在视图或助手中发生超时,则无法挽救.

Timeout :: Error都继承自Interrupt和SignalException,请正确营救.但是,抢救Exception本身确实在视图和帮助器中正确救援.

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中进行抢救.

我最终使用了这个...

rescue_from Exception do |exception|
  if exception.is_a?(Timeout::Error) || /execution expired/ =~ exception.message
    # rescued from timeout error
  end
  raise
end

更新

我修补了机架超时gem,以正确返回Timeout :: Error.这是一个线程问题.官方gem已更新: https://github.com/kch/rack-timeout

下面是新的首选方法.通常,从Exception中解脱并不是一个好主意,应该尽可能避免.

class ApplicationController < ActionController::Base
  rescue_from Timeout::Error, with: :handle_timeout

  def handle_timeout(exception)
    # handle timeout error
  end
end

I have an around_filter in my application controller to encase all actions in a timeout block, so that actions fail before hitting the 30 second Heroku limit. I also have a rescue_from Timeout::Error to cleanly rescue these timeouts. Unfortunately, the rescue_from only works some of the time.

It works fine if the timeout occurs while executing within the controllers, but fails to rescue if the timeout happens within a view or a helper.

Neither Interrupt nor SignalException, both of which Timeout::Error inherits from, rescue correctly either. However, rescuing Exception itself does rescue correctly within views and helpers.

around_filter :timeout
rescue_from Timeout::Error, :with => :timeout_rescue

def timeout
  Timeout::timeout(10){
    yield 
  }
end

def timeout_rescue
  # Rescued
end

Is there any other way to rescue Timeout::Error to get this working?

解决方案

I ran into the same problem. I'm using Rails 3 + Rack::Timeout and trying to rescue_from in ApplicationController.

I ended up using this ...

rescue_from Exception do |exception|
  if exception.is_a?(Timeout::Error) || /execution expired/ =~ exception.message
    # rescued from timeout error
  end
  raise
end

UPDATE

I patched the rack-timeout gem to properly return Timeout::Error. It was a threading issue. Official gem has been updated: https://github.com/kch/rack-timeout

The new preferred method is below. In general, it's not a great idea to rescue from Exception and should be avoided if possible.

class ApplicationController < ActionController::Base
  rescue_from Timeout::Error, with: :handle_timeout

  def handle_timeout(exception)
    # handle timeout error
  end
end

这篇关于Rescue_from不能从视图或助手中救出Timeout :: Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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