2016-03-01 51 views
1

葡萄文档说:
The rescue_from block must return a Rack::Response object, call error! or re-raise an exception.应该返回葡萄rescue_from:all block?

但如果我们用rescue_from方法只记录的东西,我们要保持原有的HTTP回答我们应该回报?

如果我重新抛出catched异常,它不会保持原来的行为。 例如,我的一些测试产生400反应,但有:

rescue_from :all do |e| 
    Rails.logger.error(e) 
    raise e 
end 

他们赶上500响应。

更好的解决办法,我发现重现原来的行为是这样的代码:

rescue_from :all do |e| 
    Rails.logger.error(e) 
    if e.respond_to?(:status) 
    error!(e.to_json, e.status, e.headers, e.backtrace) 
    else 
    error! "Internal Server Error" 
    end 
end 

我要检查,如果逮住例外响应status因为它可能是例如抛出一个RuntimeError,以及RuntimeError不回答status

我正在做这个对吗?或者有更好的解决方案来做到这一点?

回答