Sha256: 8764a56d9cb832469918483be91ab62c3e6412580833c9c48bbd8a382e703563

Contents?: true

Size: 961 Bytes

Versions: 22

Compression:

Stored size: 961 Bytes

Contents

module EasyPost
  class Error < StandardError
    attr_reader :message
    attr_reader :http_status
    attr_reader :http_body
    attr_reader :json_body
    attr_reader :param
    attr_reader :code
    attr_reader :errors

    def initialize(message=nil, http_status=nil, http_body=nil, json_body={})
      @message = message
      @http_status = http_status
      @http_body = http_body
      @json_body = json_body

      @param = @json_body.fetch(:error, {}).fetch(:param, nil)
      @code = @json_body.fetch(:error, {}).fetch(:code, nil)
      @errors = @json_body.fetch(:error, {}).fetch(:errors, nil)

      super(message)
    end

    def to_s
      s = "#{@code} (#{@http_status}): #{@message}";
      if @errors
          s += "\nField errors:\n"
          @errors.each do |field_error|
            field_error.each do |k, v|
              s += "  #{k}: #{v}\n"
            end
            s += "\n"
          end
      end
      s
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
easypost-3.0.1 lib/easypost/error.rb
easypost-3.0.0 lib/easypost/error.rb
easypost-2.7.3 lib/easypost/error.rb
easypost-2.7.2 lib/easypost/error.rb
easypost-2.7.1 lib/easypost/error.rb
easypost-2.7.0 lib/easypost/error.rb
easypost-2.6.2 lib/easypost/error.rb
easypost-2.6.1 lib/easypost/error.rb
easypost-2.6.0 lib/easypost/error.rb
easypost-2.5.0 lib/easypost/error.rb
easypost-2.4.0 lib/easypost/error.rb
easypost-2.3.0 lib/easypost/error.rb
easypost-2.2.0 lib/easypost/error.rb
easypost-2.1.11 lib/easypost/error.rb
easypost-2.1.10 lib/easypost/error.rb
easypost-2.1.9 lib/easypost/error.rb
easypost-2.1.8 lib/easypost/error.rb
easypost-2.1.7 lib/easypost/error.rb
easypost-2.1.6 lib/easypost/error.rb
easypost-2.1.5 lib/easypost/error.rb