Sha256: c879512ae92a680918b6cf53b5f1971d81e3229003388f5eb2d08afc20fb3676

Contents?: true

Size: 699 Bytes

Versions: 1

Compression:

Stored size: 699 Bytes

Contents

# frozen_string_literal: true

require 'friendly_shipping/api_error'

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

        private

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

          parsed_json = JSON.parse(error.response.body)
          parsed_json.dig("error", "message")
        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/usps_ship/api_error.rb