Dav*_*d C 2 ruby exception-handling ruby-on-rails
我目前正在使用Rails 2.3.5,我在我的应用程序中尝试尽可能多地使用rescue_from exception.
我的ApplicationController救援现在看起来像这样:
rescue_from Acl9::AccessDenied, :with => :access_denied
rescue_from Exceptions::NotPartOfGroup, :with => :not_part_of_group
rescue_from Exceptions::SomethingWentWrong, :with => :something_went_wrong
rescue_from ActiveRecord::RecordNotFound, :with => :something_went_wrong
rescue_from ActionController::UnknownAction, :with => :something_went_wrong
rescue_from ActionController::UnknownController, :with => :something_went_wrong
rescue_from ActionController::RoutingError, :with => :something_went_wrong
Run Code Online (Sandbox Code Playgroud)
我还希望能够捕获上面没有列出的任何例外情况.有没有推荐的方式我应该写我的救援?
谢谢
你可以捕捉更通用的异常,但是你必须把它们在顶部,如expained 这里
例如,要捕获所有其他异常,您可以这样做
rescue_from Exception, :with => :error_generic
rescue_from ... #all others rescues
Run Code Online (Sandbox Code Playgroud)
但是如果你这样做,请确保至少记录异常,或者你永远不会知道你的应用程序出了什么问题:
def error_generic(exception)
log_error(exception)
#your rescue code
end
Run Code Online (Sandbox Code Playgroud)
另外,您可以为一个处理程序在行中定义多个异常类:
rescue_from Exceptions::SomethingWentWrong, ActiveRecord::RecordNotFound, ... , :with => :something_went_wrong
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2448 次 |
| 最近记录: |