Add exception handlers in base API controller

* For record not found returns a 404 response
* For wrong parameters returns a 400 response
* For other exceptions returns a 500 response
This commit is contained in:
Frank Rousseau 2017-05-21 00:27:20 +02:00
parent 6cad0a965a
commit 5a0759a3d9

View file

@ -7,6 +7,26 @@ module Api
protected
rescue_from Exception do |e|
logger.error e.message
render json: {error: e.message}, status: 500
end
rescue_from ActiveRecord::RecordNotFound do
render json: {error: I18n.t("api.error.not_found")}, status: 404
end
rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: e.to_s}, status: 400
end
rescue_from ActionController::ParameterMissing do |e|
render json: {
error: I18n.t("api.error.wrong_parameters"),
message: e.message
}, status: 400
end
def current_user
current_token ? current_token.authorization.user : nil
end