Sha256: edbb045b72911c3be86b4a3a16f97597e28fed7088a7fa77686bae2764d78b18

Contents?: true

Size: 678 Bytes

Versions: 33

Compression:

Stored size: 678 Bytes

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  module Services
    class ShipEngine
      class BadRequest < StandardError
        attr_reader :rest_error, :response

        def initialize(rest_error)
          @rest_error = rest_error
          @response = rest_error.response
          super parse_json_errors || rest_error
        end

        private

        def parse_json_errors
          parsed_body = JSON.parse(response.body)
          messages = parsed_body.fetch('errors')&.map { |e| e.fetch('message') }
          messages&.join(', ')
        rescue JSON::ParserError, KeyError => _e
          nil
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
friendly_shipping-0.8.1 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.5.2 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.5 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.4.14 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.4.13 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.4.12 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.4.11 lib/friendly_shipping/services/ship_engine/bad_request.rb