Exception: Paymos::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/paymos/errors.rb,
sig/paymos.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, body, headers = {}) ⇒ ApiError

Returns a new instance of ApiError.

Parameters:

  • status (Integer)
  • body (String)
  • headers (Hash[untyped, untyped]) (defaults to: {})


15
16
17
18
19
20
21
# File 'lib/paymos/errors.rb', line 15

def initialize(status, body, headers = {})
  @status = status
  @body = body.to_s
  @headers = headers.to_h.transform_keys { |key| key.to_s.downcase }
  @problem = parse_problem(@body)
  super("Paymos API #{status}: #{detail}")
end

Instance Attribute Details

#bodyString (readonly)

Returns the value of attribute body.

Returns:

  • (String)


13
14
15
# File 'lib/paymos/errors.rb', line 13

def body
  @body
end

#headersHash[String, String] (readonly)

Returns the value of attribute headers.

Returns:

  • (Hash[String, String])


13
14
15
# File 'lib/paymos/errors.rb', line 13

def headers
  @headers
end

#problemHash[String, untyped]? (readonly)

Returns the value of attribute problem.

Returns:

  • (Hash[String, untyped], nil)


13
14
15
# File 'lib/paymos/errors.rb', line 13

def problem
  @problem
end

#statusInteger (readonly)

Returns the value of attribute status.

Returns:

  • (Integer)


13
14
15
# File 'lib/paymos/errors.rb', line 13

def status
  @status
end

Instance Method Details

#codeString

Returns:

  • (String)


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

def code
  first = errors.first
  value = first.is_a?(Hash) ? first['code'] : problem&.fetch('code', nil)
  value.to_s
end

#detailString

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
# File 'lib/paymos/errors.rb', line 39

def detail
  value = problem&.fetch('detail', nil)
  first = errors.first
  value ||= first['message'] if first.is_a?(Hash)
  value ||= code unless code.empty?
  value ||= problem&.fetch('title', nil)
  value ||= body unless body.empty?
  value || 'empty response'
end

#errorsArray[Hash[String, untyped]]

Returns:

  • (Array[Hash[String, untyped]])


23
24
25
26
# File 'lib/paymos/errors.rb', line 23

def errors
  value = problem.is_a?(Hash) ? problem['errors'] : nil
  value.is_a?(Array) ? value : []
end

#fieldObject

Returns:

  • (Object)


34
35
36
37
# File 'lib/paymos/errors.rb', line 34

def field
  first = errors.first
  first.is_a?(Hash) ? first['field'] : problem&.fetch('field', nil)
end

#retry_after_seconds(now: Time.now) ⇒ Integer?

Parameters:

  • now: (Time) (defaults to: Time.now)

Returns:

  • (Integer, nil)


49
50
51
52
53
54
55
56
57
# File 'lib/paymos/errors.rb', line 49

def retry_after_seconds(now: Time.now)
  value = headers['retry-after']
  return nil if value.nil? || value.empty?
  return value.to_i if value.match?(/\A\d+\z/)

  [(Time.httpdate(value) - now).ceil, 0].max
rescue ArgumentError
  nil
end