2011-03-10 11 views
10

मैं अपवाद_नोटिफायर को लागू करने की कोशिश कर रहा हूं और एक कस्टम अपवाद हैंडलिंग मेरी रेल 3 एप में हैंडलिंग। जब मैं केवल अपवाद नोटिफ़ायर का उपयोग कर रहा हूं तो सब कुछ ठीक काम करता है। अपवाद नोटिफ़ायर और save_from

config.consider_all_requests_local = false 

साथ विकास मोड और मेरे application_controller में एक rescue_from में: मेरे application.rb

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

केवल समस्या में

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 

, प्रतीत हो रहा है कि विकल्प नहीं हैं request.env में लोड किया गया। मैंने फ़ाइल को एक अतिरिक्त प्रारंभकर्ता में करने की कोशिश की और मुझे नहीं पता कि और क्या - यह काम नहीं कर रहा है। इस समय मेरे पास वास्तव में बदसूरत हैक है, जहां मैं ईमेल वितरित करने से पहले हैश के साथ request.env को विलय करता हूं .. कोई विचार?

+1

क्या आपको कभी इस समस्या का हल मिल गया है? मेरे पास भी यह है कि आप जो भी मदद कर सकते हैं उसकी बहुत सराहना की जा रही है! –

+0

नहीं, मुझे request.env को हाइजैक करना था और उन्हें मैन्युअल रूप से सेट करना था – Clint

+0

क्या आप इसका उदाहरण पोस्ट कर सकते हैं कि आपने यह कैसे किया? एक बार फिर धन्यवाद! –

उत्तर

7

अपवाद_नोटिफिकेशन रेल 3 में मिडलवेयर है, इसलिए विकल्प सीधे उस वर्ग पर सेट होते हैं जो कॉल को संसाधित करता है और वह वर्ग उन्हें पर्यावरण में तब तक सेट नहीं करता जब तक कि यह अपवाद नहीं लेता (see here)। This fork एक पृष्ठभूमि_exception_notification विधि जो आप उपयोग कर सकते हैं जोड़ता है। मैंने विचार उधार लिया और बस इस सहायक विधि को जोड़ा:

def background_exception_notification(env, exception) 
    if notifier = Rails.application.config.middleware.detect { |x| x.klass == ExceptionNotifier } 
    env['exception_notifier.options'] = notifier.args.first || {}     
    ExceptionNotifier::Notifier.exception_notification(env, exception).deliver 
    env['exception_notifier.delivered'] = true 
    end 
end 
+0

क्या आपने आधिकारिक रेपो (आजकल https://github.com/smartinez87/exception_notification) को पुल अनुरोध जारी करने पर विचार किया है? –

संबंधित मुद्दे