简体   繁体   中英

ExceptionNotifier and rescue_from

I am trying to implement exception_notifier and a custom exception handling in my rails 3 app. 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:

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

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. 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?

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 ). This fork adds a background_exception_notification method that you can use. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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