Sha256: 0727fe99ff0d27f29e38bf6473b4edc134d3be6dcd06737050838edf40c67369

Contents?: true

Size: 651 Bytes

Versions: 2

Compression:

Stored size: 651 Bytes

Contents

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 => _error
          nil
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
friendly_shipping-0.2.1 lib/friendly_shipping/services/ship_engine/bad_request.rb
friendly_shipping-0.2.0 lib/friendly_shipping/services/ship_engine/bad_request.rb