# frozen_string_literal: true module MinatoErrorHandler module ErrorHandler extend ActiveSupport::Concern included do |base| rescue_from StandardError, with: :handle base.extend ClassMethods end private def handle(exception) error = parse_error(exception) report_error error render_error error end def parse_error(error) if error.is_a?(MinatoErrorHandler::Errors::MinatoError) error.http_request = request if error.http_request.nil? return error end error_class = self.class.default_error_class || MinatoErrorHandler::Errors::UnknowError minato_error = error_class.new minato_error.caused_by = error.full_message minato_error.backtrace = error.backtrace minato_error.http_request = request minato_error end def report_error(error) Rails.logger.error(error.as_json) end def render_error(error) render json: { errors: [error.json_response] }, status: error.status_code end module ClassMethods def default_error(klass) # rubocop:disable Style/ClassVars class_variable_set(:@@default_error_class, klass.to_s.camelize.constantize) # rubocop:enable Style/ClassVars end def default_error_class return nil unless class_variable_defined? :@@default_error_class class_variable_get(:@@default_error_class) end end end end