Sha256: a46676b110482696ca49c420d791cafc10e721a6e7e98ab7c37d1243bd38083e

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module GoPuff
  module TaxService
    module Errors
      class Base < StandardError; end

      class FeesKindInvalid < Base
        def initialize(kind)
          super("Invalid fees_kind attribute: #{kind}")
        end
      end

      class Unauthorized < Base
        def initialize(token = nil)
          super("Authentication failed with token: #{token}")
        end
      end

      class RequestFailed < Base
        def initialize(error, attempts)
          super("Request to tax service failed after #{attempts} attempts. Error: #{error&.class} #{error&.message}")
        end
      end

      class ValidationFailed < Base
        def initialize(response, params)
          @response = response
          @params = params

          super(error_message)
        end

        def error_message
          errors = extract_messages_from_error_response(@response)

          "Tax service request failed: #{errors}. Sent params: #{@params}"
        end

        def extract_messages_from_error_response(error_hash)
          return '' unless error_hash.is_a?(Hash)

          messages = []

          error_hash.each do |key, value|
            if key.to_sym == :message
              messages << value
            elsif value.is_a?(Hash)
              messages << extract_messages_from_error_response(value)
            end
          end

          messages.flatten.join('; ')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
go_puff-tax_service-1.5.0 lib/go_puff/tax_service/errors.rb