我正在尝试实现自定义错误处理以及使用 CanCan。当用户到达不允许访问的区域时,会引发 CanCan::AccessDenied 错误,并且应将其发送到根 url。相反,“rescue_from Exception”捕获 CanCan::AccessDenied 并且用户收到 500 错误。我究竟做错了什么?
#application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, :alert => exception.message
end
rescue_from Exception,
:with => :render_error
rescue_from Mongoid::Errors::DocumentNotFound,
:with => :render_not_found
rescue_from ActionController::RoutingError,
:with => :render_not_found
rescue_from ActionController::UnknownController,
:with => :render_not_found
rescue_from AbstractController::ActionNotFound,
:with => :render_not_found
def render_not_found(exception)
render :template => "/errors/404.html",
:layout => 'errors.html',
:status => 404
end
def render_error(exception)
render :template => "/errors/500.html",
:layout => 'errors.html',
:status => 500
end
您是否尝试从异常/错误中重新排序rescue_,首先更通用,稍后更具体,例如
rescue_from StandardError,
:with => :render_error
rescue_from Mongoid::Errors::DocumentNotFound,
:with => :render_not_found
rescue_from ActionController::RoutingError,
:with => :render_not_found
rescue_from ActionController::UnknownController,
:with => :render_not_found
rescue_from AbstractController::ActionNotFound,
:with => :render_not_found
rescue_from CanCan::AccessDenied do |exception|
redirect_to main_app.root_url, :alert => exception.message
end
注意:您可能想用 StandardError 替换通用异常。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)