Sha256: b20bbfc6576acd122f8db153ac49619f422bcac1bd4653b8382b6294782500c6

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'friendly_shipping/api_error'

module FriendlyShipping
  module Services
    class TForceFreight
      # Raised when an API error is returned.
      class ApiError < FriendlyShipping::ApiError
        # @param cause [RestClient::Exception]
        def initialize(cause)
          super(cause, parse_message(cause))
        end

        private

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

          parsed_json = JSON.parse(error.response.body)
          if parsed_json['summary'].present?
            status = parsed_json.dig("summary", "responseStatus", "code")
            message = parsed_json.dig("summary", "responseStatus", "message").presence ||
                      parsed_json.dig("summary", "responseStatus", "description")
          elsif parsed_json['responseStatus'].present?
            status = parsed_json.dig("responseStatus", "code")
            message = parsed_json.dig("responseStatus", "description")
          else
            status = parsed_json['statusCode']
            message = parsed_json['message'].presence ||
                      parsed_json['description']
          end
          [status, message].compact.join(": ")
        rescue JSON::ParserError, KeyError => _e
          nil
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/tforce_freight/api_error.rb