Sha256: 15753dd4b40ceeeac145cc9f7b3f910c735ce9011abbfe0e10eef8d641447ef6

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'friendly_shipping/api_error'

module FriendlyShipping
  module Services
    class UpsFreight
      class ApiError < FriendlyShipping::ApiError
        # @param [RestClient::Exception] cause
        def initialize(cause)
          super cause, parse_message(cause)
        end

        private

        # @param [RestClient::Exception] cause
        def parse_message(cause)
          return cause.message unless cause.response

          parsed_json = JSON.parse(cause.response.body)

          if parsed_json['httpCode'].present?
            status = [parsed_json['httpCode'], parsed_json['httpMessage']].compact.join(" ")
            desc = parsed_json['moreInformation']
            [status, desc].compact.join(": ")
          else
            errors = parsed_json.dig('response', 'errors') || []
            errors.map do |err|
              status = err['code']
              desc = err['message']
              [status, desc].compact.join(": ").presence || "UPS Freight could not process the request."
            end.join("\n")
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
friendly_shipping-0.8.1 lib/friendly_shipping/services/ups_freight/api_error.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/ups_freight/api_error.rb