2016-03-01 47 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

我正在做這個對嗎?或者有更好的解決方案來做到這一點?

回答