Rails 似乎没有映射异常:unauthorized
.
默认映射定义在activerecord/lib/active_record/railtie.rb https://github.com/rails/rails/blob/4-1-stable/activerecord/lib/active_record/railtie.rb#L25:
config.action_dispatch.rescue_responses.merge!(
'ActiveRecord::RecordNotFound' => :not_found,
'ActiveRecord::StaleObjectError' => :conflict,
'ActiveRecord::RecordInvalid' => :unprocessable_entity,
'ActiveRecord::RecordNotSaved' => :unprocessable_entity
)
and actionpack/lib/action_dispatch/middleware/exception_wrapper.rb https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb#L8:
@@rescue_responses.merge!(
'ActionController::RoutingError' => :not_found,
'AbstractController::ActionNotFound' => :not_found,
'ActionController::MethodNotAllowed' => :method_not_allowed,
'ActionController::UnknownHttpMethod' => :method_not_allowed,
'ActionController::NotImplemented' => :not_implemented,
'ActionController::UnknownFormat' => :not_acceptable,
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
'ActionDispatch::ParamsParser::ParseError' => :bad_request,
'ActionController::BadRequest' => :bad_request,
'ActionController::ParameterMissing' => :bad_request
)
您可以从应用程序的配置中添加自定义异常(或自定义异常)Railtie http://api.rubyonrails.org/classes/Rails/Railtie.html):
Your::Application.configure do
config.action_dispatch.rescue_responses.merge!(
'AuthorizationException' => :unauthorized
)
# ...
end
或者简单地使用rescue_from
.