【问题标题】:ExceptionNotifier and rescue_fromExceptionNotifier 和rescue_from
【发布时间】:2011-07-12 18:25:36
【问题描述】:

我正在尝试实现 exception_notifier 和自定义异常处理 在我的 Rails 3 应用程序中。当我只使用异常通知器时,一切正常。 在开发模式下使用

config.consider_all_requests_local = false

和我的 application_controller 中的一个 rescue_from:

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

在我的 application.rb 中

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

似乎唯一的问题是,选项没有加载到 request.env 中。 我在一个额外的初始化程序中尝试了该文件,但我不知道还有什么 - 它不起作用。 目前我有一个非常丑陋的黑客,我将 request.env 与 在发送电子邮件之前哈希.. 有什么想法吗?

【问题讨论】:

  • 你有没有找到解决这个问题的方法?我也有,因此非常感谢您提供的任何帮助!
  • 不,我不得不劫持 request.env 并手动设置它们
  • 你能举个例子说明你是怎么做到的吗?再次感谢!

标签: ruby-on-rails ruby-on-rails-3 exception error-handling


【解决方案1】:

exception_notification 是 Rails 3 中的中间件,因此选项直接在处理调用的类上设置,并且该类不会在环境中设置它们,除非它捕获到异常 (see here)。 This fork 添加了一个你可以使用的 background_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

【讨论】:

猜你喜欢
  • 2013-07-30
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 2017-12-23
  • 2013-04-17
相关资源
最近更新 更多