Sha256: 0c1e95510ecd2d45760ce0fd05b30afb3854c9f886970ac159c235c91dbac88e

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module LedgerSync
  module Ledgers
    module QuickBooksOnline
      class Util
        class ErrorMatcher
          attr_reader :error,
                      :message

          def initialize(error:)
            @error = error
            @message = error.message.to_s
          end

          def body
            @body ||= error.response.body
          rescue NoMethodError
            nil
          end

          def error_class
            raise NotImplementedError
          end

          def error_message
            return error.message unless body

            fault&.fetch('message', nil) ||
              fault&.fetch('Message', nil) ||
              parsed_body['error']
          end

          def detail
            return if body.blank?

            fault&.fetch('detail') ||
              fault&.fetch('Detail')
          end

          def code
            return if body.blank?

            fault&.fetch('code') ||
              fault&.fetch('code').to_i
          end

          def match?
            raise NotImplementedError
          end

          def parsed_body
            @parsed_body ||= JSON.parse(body)
          end

          def output_message
            raise NotImplementedError
          end

          private

          def fault
            parsed_body.dig('fault', 'error')&.first ||
              parsed_body.dig('Fault', 'Error')&.first
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ledger_sync-1.6.0 lib/ledger_sync/ledgers/quickbooks_online/util/error_matcher.rb
ledger_sync-1.5.2 lib/ledger_sync/ledgers/quickbooks_online/util/error_matcher.rb
ledger_sync-1.5.1 lib/ledger_sync/ledgers/quickbooks_online/util/error_matcher.rb
ledger_sync-1.5.0 lib/ledger_sync/ledgers/quickbooks_online/util/error_matcher.rb