Exception: Balanced::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/balanced/error.rb,
lib/balanced/resources/order.rb

Overview

Custom error class for rescuing from all API response-related Balanced errors

Defined Under Namespace

Classes: OrderCancelFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • response (Hash) (defaults to: nil)

    the decoded json response body



8
9
10
11
12
13
# File 'lib/balanced/error.rb', line 8

def initialize(response=nil)
  @response = response
  unless response.nil?
    super error_message
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#bodyHash

Returns:

  • (Hash)


16
17
18
19
20
21
# File 'lib/balanced/error.rb', line 16

def body
  @body ||= begin
    return {} unless response[:body]
    Utils.indifferent_read_access(response[:body])
  end
end

#error_messageObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/balanced/error.rb', line 23

def error_message
  set_attrs
  errors = body.fetch('errors', nil)
  unless errors.nil?
    error = body[:errors][0]
    extra = error[:additional] ? " -- #{error[:additional]}" : ""
    "#{self.class.name}(#{response[:status]})::#{error[:status]}:: "\
    "#{response[:method].to_s.upcase} #{response[:url].to_s}: "\
    "#{error[:category_code]}: #{error[:description]} #{extra}"
  end
end