开发者

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_notificatio开发者_运维百科n(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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜