【发布时间】:2014-09-09 13:09:09
【问题描述】:
我有一个 Rails 应用程序,我试图在我的共享主机上的数据库服务器出现故障时捕获异常,以便我可以了解我的服务器的总体性能。为此,我从https://github.com/smartinez87/exception_notification 设置了 exception_notification gem,在我的 ApplicationController 中,我有以下代码:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :check_database
rescue_from Exception do |exception|
ExceptionNotifier::Notifier \
.exception_notification(request.env, exception) \
.deliver
raise exception
end
private
def check_database
Category.first
rescue Exception => exception
ExceptionNotifier::Notifier \
.exception_notification(request.env, exception) \
.deliver
raise exception
end
end
但是,没有一个救援块对我有用。您能否建议我如何在 Rails 中捕获这样的异常?谢谢
【问题讨论】:
-
你是什么意思不是他们没有工作?同样为了救援,我相信你需要一个
begin,例如begin; Category.first; rescue ...另外,如果您知道要挽救的异常类型,我会明确指定它,尽管这不是必要的,因为您在记录后正确地重新引发。 -
当然,我会尝试捕获与我的应用程序相关的最具体的异常。但是,这仅用于测试,因为它不起作用,我不得不尝试将 Exception 作为所有异常的根类。我说它不起作用,因为当我尝试关闭数据库服务器时,没有任何代码块向我的电子邮件地址发送有关错误的电子邮件。相反,它只是在访问时显示错误页面。
标签: ruby-on-rails ruby exception-handling