我的Rails 4應用程序使用RocketPants爲其JSON API和權威授權。如何在Rails rescue_from語句中重新引發Ruby異常?
我有我的/app/controllers/api/v1/base_controller.rb
文件中的代碼來處理來自Pundit的錯誤。每當用戶無權更新資源,權威人士拋出一個異常NotAuthorizedError
,我與我的user_not_authorized
方法挽救:
class API::V1::BaseController < RocketPants::Base
include Pundit
version 1
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
error! :forbidden
end
end
當我致電error!
方法RocketPants provides從我的異常處理程序,我期望能獲得這樣的JSON響應:
{
"error": "forbidden",
"error_description": "The requested action was forbidden."
}
相反,但是,調用error!
只是立即炸燬要求:
Completed 500 Internal Server Error in 143ms
RocketPants::Forbidden - RocketPants::Forbidden:
rocket_pants (1.13.1) lib/rocket_pants/controller/error_handling.rb:44:in `error!'
app/controllers/api/v1/base_controller.rb:61:in `user_not_authorized'
滿堆棧跟蹤here。
爲什麼error!
方法在我的Pundit異常處理程序中調用時應該如何執行?
如果我把error! :forbidden
置於我的控制器操作的中間,它按預期工作。
爲背景,從base_controller.rb
繼承和調用權威人士的authorize
方法控制器看起來是這樣的:
class API::V1::MealsController < API::V1::BaseController
before_filter :find_entity
def create
meal = @entity.meals.build(meal_params)
authorize(@entity, :update?)
if meal.save
expose meal, status: :created
else
expose meal.errors, status: 422
end
end
end