app/controllers/concerns/api/render_error_concern.rb in captive-api-0.2.12 vs app/controllers/concerns/api/render_error_concern.rb in captive-api-1.0.0

- old
+ new

@@ -1,46 +1,38 @@ # frozen_string_literal: true -module API - ## - # Permet de catcher des erreurs pour rendre une page d'erreur en json +module Api module RenderErrorConcern extend ActiveSupport::Concern included do rescue_from ActiveRecord::RecordNotFound, with: :not_found - rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing - rescue_from CanCan::AccessDenied do |exception| render_error_from_exception(exception, status: :forbidden) end protected def render_error_from_object( object, status: :unprocessable_entity, - message: "Erreur de validation" + message: 'Erreur de validation' ) errors = object.errors.messages - Rails.logger.error("Impossible de sauvegarder l'objet #{object.class} : #{errors}") + Rails.logger.error("Impossible de sauvagarder l'objet #{object.class} : #{errors}") render_error( - message: message, errors: errors, status: status + message: message, errors: errors, status: :unprocessable_entity ) end def render_error_from_exception(exception, status:, message: nil) render_error( message: exception.message, errors: exception.message, status: status ) end - def handle_parameter_missing(exception) - render_error_from_exception(exception, status: :bad_request) - end - - def not_found(exception) - render_error(message: "#{exception.model} not found", status: :not_found) + def not_found + render_error(message: 'Erreur not found', status: :not_found) end def render_error(message:, status:, errors: [], code: nil) code ||= status.to_s render json: {