Exception: PaymentKit::Error

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

Overview

Base error for all PaymentKit failures.

PaymentKit returns RFC 7807 problem documents. Standard members (+title+, detail, status, instance, request_id) plus any extension members are kept in problem so callers can branch on them without re-parsing the body.

Transient failures carry a top-level error_code and retryable: true (for example invoice_locked on HTTP 409).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, body: nil, request_id: nil, error_code: nil, retryable: nil) ⇒ Error

Any attribute not passed explicitly is read from the RFC 7807 body.



31
32
33
34
35
36
37
38
39
40
# File 'lib/payment_kit/errors.rb', line 31

def initialize(message = nil, status: nil, body: nil, request_id: nil,
               error_code: nil, retryable: nil)
  @status = status
  @body = body
  @problem = parse_problem(body)
  @request_id = request_id || @problem["request_id"]
  @error_code = error_code || @problem["error_code"]
  @retryable = retryable.nil? ? @problem["retryable"] : retryable
  super(message)
end

Instance Attribute Details

#bodyObject (readonly)

Raw response body as received.



19
20
21
# File 'lib/payment_kit/errors.rb', line 19

def body
  @body
end

#error_codeObject (readonly)

Machine-readable code such as invoice_locked, when PaymentKit sends one.



25
26
27
# File 'lib/payment_kit/errors.rb', line 25

def error_code
  @error_code
end

#problemObject (readonly)

Parsed RFC 7807 problem document as a Hash; empty when the body was not JSON.



28
29
30
# File 'lib/payment_kit/errors.rb', line 28

def problem
  @problem
end

#request_idObject (readonly)

PaymentKit request id, from the problem document or the Request-Id header.



22
23
24
# File 'lib/payment_kit/errors.rb', line 22

def request_id
  @request_id
end

#statusObject (readonly)

HTTP status of the failing response, or nil for client-side failures.



16
17
18
# File 'lib/payment_kit/errors.rb', line 16

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Reads an RFC 7807 extension member, e.g. error["invoice_id"].



48
49
50
# File 'lib/payment_kit/errors.rb', line 48

def [](key)
  @problem[key.to_s]
end

#invoice_idObject

Convenience reader for the invoice_id extension member, when present.



53
54
55
# File 'lib/payment_kit/errors.rb', line 53

def invoice_id
  self["invoice_id"]
end

#retryable?Boolean

Whether PaymentKit marked this failure as safe to retry unchanged.

Returns:

  • (Boolean)


43
44
45
# File 'lib/payment_kit/errors.rb', line 43

def retryable?
  @retryable == true
end

#subscription_idObject

Convenience reader for the subscription_id extension member, when present.



58
59
60
# File 'lib/payment_kit/errors.rb', line 58

def subscription_id
  self["subscription_id"]
end