diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e384209..3477ee0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -59,26 +59,28 @@ def transform_params params.transform_keys! { |key| key.tr('-', '_') } end - rescue_from *RESCUABLE_EXCEPTIONS do |exception| - status = case exception.class.to_s - when "CanCan::AccessDenied", "JWT::DecodeError","JWT::VerificationError" then 401 - when "ActiveRecord::RecordNotFound", "AbstractController::ActionNotFound", "ActionController::RoutingError" then 404 - when "ActiveRecord::RecordNotUnique" then 409 - when "ActiveModel::ForbiddenAttributesError", "ActionController::ParameterMissing", "ActionController::UnpermittedParameters", "NoMethodError", "ActiveRecord::RecordInvalid", "JSON::ParserError" then 422 - else 400 - end - - if status == 404 - message = "The resource you are looking for doesn't exist." - elsif status == 401 - message = "You are not authorized to access this resource." - elsif status == 406 - message = "The content type is not recognized." - elsif status == 409 - message = "The resource already exists." - else - message = exception.message - end + rescue_from(*RESCUABLE_EXCEPTIONS) do |exception| + status = + case exception.class.to_s + when "CanCan::AccessDenied", "JWT::DecodeError","JWT::VerificationError" then 401 + when "ActiveRecord::RecordNotFound", "AbstractController::ActionNotFound", "ActionController::RoutingError" then 404 + when "ActiveRecord::RecordNotUnique" then 409 + when "ActiveModel::ForbiddenAttributesError", "ActionController::ParameterMissing", "ActionController::UnpermittedParameters", "NoMethodError", "ActiveRecord::RecordInvalid", "JSON::ParserError" then 422 + else + 400 + end + + if status == 404 + message = "The resource you are looking for doesn't exist." + elsif status == 401 + message = "You are not authorized to access this resource." + elsif status == 406 + message = "The content type is not recognized." + elsif status == 409 + message = "The resource already exists." + else + message = exception.message + end render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status end