Exception: GoCardlessPro::Error

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

Overview

A class to represent an API Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Error

intialize a new error  @param error the error from the API

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/gocardless_pro/error.rb', line 8

def initialize(error)
  raise ArgumentError, 'GoCardless errors expect a hash' unless error.is_a?(Hash)
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



4
5
6
# File 'lib/gocardless_pro/error.rb', line 4

def error
  @error
end

Instance Method Details

#codeObject

access the code from the response



39
40
41
# File 'lib/gocardless_pro/error.rb', line 39

def code
  @error['code']
end

#documentation_urlObject

access the documentation_url from the response



14
15
16
# File 'lib/gocardless_pro/error.rb', line 14

def documentation_url
  @error['documentation_url']
end

#errorsObject

access the errors from the response



49
50
51
# File 'lib/gocardless_pro/error.rb', line 49

def errors
  @error.fetch('errors', [])
end

#messageObject

access the message from the response



19
20
21
# File 'lib/gocardless_pro/error.rb', line 19

def message
  @error['message']
end

#request_idObject

access the request_id from the response



44
45
46
# File 'lib/gocardless_pro/error.rb', line 44

def request_id
  @error['request_id']
end

#to_sObject



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

def to_s
  if errors.any?
    errors.
      map { |err| "#{err['field']} #{err['message']}" }.
      join(', ')
  else
    @error['message']
  end
end

#typeObject

access the type from the response



34
35
36
# File 'lib/gocardless_pro/error.rb', line 34

def type
  @error['type']
end