【发布时间】:2009-10-01 16:01:16
【问题描述】:
我在我的应用程序中使用 Rails exception_notification 插件,发现它非常有用。
但是,有时我想捕获异常并优雅地处理它,但仍想收到异常通知电子邮件。它似乎只发送未捕获的异常。
有谁知道当您已经发现异常时如何强制发送电子邮件?
【问题讨论】:
标签: ruby-on-rails exception-handling
我在我的应用程序中使用 Rails exception_notification 插件,发现它非常有用。
但是,有时我想捕获异常并优雅地处理它,但仍想收到异常通知电子邮件。它似乎只发送未捕获的异常。
有谁知道当您已经发现异常时如何强制发送电子邮件?
【问题讨论】:
标签: ruby-on-rails exception-handling
我想出了如何做到这一点。这是您将放入控制器中以触发电子邮件的代码。
对于Rails 2.3 version of the Exception_Notification plugin:
begin
10 / 0
rescue Exception => e
ExceptionNotifier.deliver_exception_notification(e, self, request)
end
对于Rails 3 version of the Exception_Notification plugin:
begin
10 / 0
rescue Exception => e
ExceptionNotifier::Notifier.exception_notification(request.env, e).deliver
end
对于Rails 4 version (currently v4.0.1 of the exception_notification gem):
begin
some code...
rescue => e
ExceptionNotifier.notify_exception(e)
ExceptionNotifier.notify_exception(e, env: request.env, data: { message: "oops" })
end
【讨论】:
{} 而不是request.env,尽管在电子邮件中有一个关于无法访问会话和请求数据的错误。
Exception Notifier 是专门为捕获未捕获的错误而设计的。一旦您发现错误,您可以自己发送电子邮件。快速而肮脏的方法是在捕获异常时触发异常邮件程序代码。我不记得这个方法是如何从我的脑海中浮现出来的,但是快速浏览一下插件应该会给你带来结果。查找邮件程序代码的 render_exception_in_public(或类似的东西)。
【讨论】: