Exception: EasyPost::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/easypost/error.rb

Overview

EasyPost Error object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status = nil, code = nil, errors = nil, http_body = nil) ⇒ Error

Initialize a new EasyPost Error



8
9
10
11
12
13
14
15
16
17
# File 'lib/easypost/error.rb', line 8

def initialize(message = nil, status = nil, code = nil, errors = nil, http_body = nil)
  # message should be a string but can sometimes incorrectly come back as an array
  @message = message.is_a?(String) ? message : EasyPost::Error.traverse_json_element(message, [])
  @status = status
  @code = code
  @errors = errors
  @http_body = http_body

  super(message)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/easypost/error.rb', line 5

def code
  @code
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/easypost/error.rb', line 5

def errors
  @errors
end

#http_bodyObject (readonly)

Returns the value of attribute http_body.



5
6
7
# File 'lib/easypost/error.rb', line 5

def http_body
  @http_body
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/easypost/error.rb', line 5

def message
  @message
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/easypost/error.rb', line 5

def status
  @status
end

Class Method Details

.traverse_json_element(error_message, messages_list) ⇒ Object

Recursively traverses a JSON element to extract error messages and returns them as a comma-separated string.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/easypost/error.rb', line 34

def self.traverse_json_element(error_message, messages_list)
  case error_message
  when Hash
    error_message.each_value { |value| traverse_json_element(value, messages_list) }
  when Array
    error_message.each { |value| traverse_json_element(value, messages_list) }
  else
    messages_list.push(error_message.to_s)
  end

  messages_list.join(', ')
end

Instance Method Details

#==(other) ⇒ Object

Compare error properties.



25
26
27
28
29
30
31
# File 'lib/easypost/error.rb', line 25

def ==(other)
  other.is_a?(EasyPost::Error) &&
    message == other.message &&
    status == other.status &&
    code == other.code &&
    errors == other.errors
end

#to_sObject

Convert an error to a string.



20
21
22
# File 'lib/easypost/error.rb', line 20

def to_s
  "#{code} (#{status}): #{message} #{errors}".strip
end