简体   繁体   English

ExceptionNotifier和rescue_from

[英]ExceptionNotifier and rescue_from

I am trying to implement exception_notifier and a custom exception handling in my rails 3 app. 我正在尝试在Rails 3应用程序中实现exception_notifier和自定义异常处理。 When I am only using exception notifier everything works fine. 当我仅使用异常通知程序时,一切正常。 In development mode with 在开发模式下

config.consider_all_requests_local = false

and a rescue_from in my application_controller: 和我的application_controller中的一次抢救:

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

in my application.rb 在我的application.rb中

config.middleware.use ExceptionNotifier,
  :email_prefix => "Error: ",
  :sender_address => %{"notifier" <notifier@wannagohome.com>},
  :exception_recipients => %w{ myself@fail.com }

The only problem seems to be, that the options are not loaded into the request.env. 唯一的问题似乎是,这些选项未加载到request.env中。 I tried the file in a extra initializer and I don't know what else - it's not working. 我在额外的初始化程序中尝试了该文件,但我不知道还有什么-它无法正常工作。 At the moment I have a really ugly hack, where I merge the request.env with a hash before delivering the email.. Any idea? 目前,我遇到了一个非常丑陋的黑客,在将电子邮件发送之前,我将request.env与哈希值合并。

exception_notification is middleware in Rails 3 so the options are set directly on the class that processes the call and that class doesn't set them in the environment unless it catches an exception ( see here ). exception_notification是Rails 3中的中间件,因此选项直接在处理调用的类上设置,并且该类不会在环境中设置它们,除非它捕获到异常( 请参见此处 )。 This fork adds a background_exception_notification method that you can use. 此派生添加了可以使用的background_exception_notification方法。 I borrowed the idea and just added this helper method: 我借用了这个想法,只是添加了这个辅助方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM