Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" <[email protected]>},
  :exception_recipients => %w{ [email protected] }

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?

like image 898
Clint Avatar asked Mar 10 '11 14:03

Clint


1 Answers

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
like image 139
Brian Deterling Avatar answered Oct 20 '22 00:10

Brian Deterling