Sha256: b55cc9355df7bda3d98f11b1205a6c670637b3631e7e923d15232fe6233a72b9

Contents?: true

Size: 561 Bytes

Versions: 4

Compression:

Stored size: 561 Bytes

Contents

# frozen_string_literal: true

require 'json'

module FriendlyShipping
  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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
friendly_shipping-0.3.0 lib/friendly_shipping/bad_request.rb
friendly_shipping-0.2.6 lib/friendly_shipping/bad_request.rb
friendly_shipping-0.2.5 lib/friendly_shipping/bad_request.rb
friendly_shipping-0.2.4 lib/friendly_shipping/bad_request.rb