diaspora/app/controllers/api/v0/base_controller.rb
Frank Rousseau 5a0759a3d9 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
2018-12-30 22:31:33 +01:00

35 lines
876 B
Ruby

# frozen_string_literal: true
module Api
module V0
class BaseController < ApplicationController
include Api::OpenidConnect::ProtectedResourceEndpoint
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
end
end
end