2011-03-10 13 views
10

我想在我的rails 3應用程序中實現exception_notifier和自定義異常處理 。當我只使用異常通知器時,一切正常。 在發展模式ExceptionNotifier和rescue_from

config.consider_all_requests_local = false 

,並在我的application_controller一個rescue_from:

unless Rails.application.config.consider_all_requests_local 
    rescue_from Exception, :with => :render_error 
end 

def render_error(exception) 
    ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver 
end 

在我的application.rb中

config.middleware.use ExceptionNotifier, 
    :email_prefix => "Error: ", 
    :sender_address => %{"notifier" <[email protected]>}, 
    :exception_recipients => %w{ [email protected] } 

唯一的問題似乎是,該選項不加載到request.env中。 我在一個額外的初始化程序中試過這個文件,我不知道還有什麼 - 它不工作。 目前我有一個非常醜陋的黑客,在發送郵件之前,我將request.env與 哈希合併。 任何想法?

+1

你曾經找到解決這個問題的方法嗎?我也有,所以你可以提供的任何幫助非常感謝! – 2011-05-07 20:17:04

+0

不,我不得不劫持request.env並手動設置它們 – Clint 2011-05-18 16:01:32

+0

你能發表一個你如何做的例子嗎?再次感謝! – 2011-05-23 05:55:06

回答

7

exception_notification是Rails 3中的中間件,因此這些選項直接在處理調用的類上設置,並且該類不會在環境中設置它們,除非它捕獲到異常(see here)。 This fork添加您可以使用的background_exception_notification方法。我借用了這個想法,並添加了這個輔助方法:

​​3210
+0

您是否考慮過向官方回購協議發佈pull request(時下https://github.com/smartinez87/exception_notification)? – 2011-09-07 09:03:52

相關問題