Sha256: 2cae9c847147484ebb2433dc6607c4a6b0bfa8be3305ba54dcd3fde020f5deaa

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

module ApiPack
  module Errors
    class HandleError
      include Errors::ErrorMap

      def initialize(error)
        @error = error
      end

      def call
        handle_error
      end

      private

      attr_accessor :error

      def handle_error
        if ERROR_MAP.keys.include? error.class.name
          handler = ERROR_MAP[error.class.name]
          body = send(handler[:method], handler: handler, error: error)
        else
          body = {
            title: 'Internal Server Error',
            details: error.message,
            status: :internal_server_error
          }
        end

        { body: serialize(body), status: body[:status] }
      end

      def serialize(body)
        ApiPack::Errors::ApiErrorsSerializer.new(body).serializer
      end

      def error_message_body(handler:, error:)
        {
          title: handler[:title],
          status: handler[:status],
          details: error.message
        }
      end

      def parameter_missing(handler:, error:)
        {
          title: handler[:title],
          details: error.message,
          status: handler[:status],
          parameter: error.param
        }
      end

      def record_invalid(handler:, error:)
        {
          title: handler[:title],
          status: handler[:status],
          details: ApiPack::Errors::ValidationErrorsSerializer.new(error.record).serialize
        }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
api_pack-1.3.1 lib/api_pack/errors/handle_error.rb
api_pack-1.2.1 lib/api_pack/errors/handle_error.rb
api_pack-1.1.4 lib/api_pack/errors/handle_error.rb
api_pack-1.1.3 lib/api_pack/errors/handle_error.rb
api_pack-1.1.2 lib/api_pack/errors/handle_error.rb
api_pack-1.1.1 lib/api_pack/errors/handle_error.rb
api_pack-1.0.1 lib/api_pack/errors/handle_error.rb
api_pack-1.0.0 lib/api_pack/errors/handle_error.rb