Exception: Bunq::ResponseError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/bunq/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = 'Response error', code: nil, headers: nil, body: nil) ⇒ ResponseError

Returns a new instance of ResponseError.



9
10
11
12
13
14
# File 'lib/bunq/errors.rb', line 9

def initialize(msg = 'Response error', code: nil, headers: nil, body: nil)
  @code = code
  @headers = headers || {}
  @body = body
  super("#{msg} (code: #{code}, body: #{body})")
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/bunq/errors.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/bunq/errors.rb', line 5

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/bunq/errors.rb', line 6

def headers
  @headers
end

Instance Method Details

#errorsArray|nil

Returns an array of errors returned from the API, or nil if no errors are returned.

Returns:

  • (Array|nil)


29
30
31
32
# File 'lib/bunq/errors.rb', line 29

def errors
  json = parsed_body
  json ? json['Error'] : nil
end

#parsed_body(opts = {}) ⇒ Object

Returns the parsed body if it is a JSON document, nil otherwise.

Parameters:

  • opts (Hash) (defaults to: {})

    Optional options that are passed to ‘JSON.parse`.



18
19
20
21
22
23
24
25
# File 'lib/bunq/errors.rb', line 18

def parsed_body(opts = {})
  if @body && @headers['content-type'] && @headers['content-type'].include?('application/json')
    JSON.parse(
      @body,
      opts,
    )
  end
end