Exception: Paymos::ApiError
- Defined in:
- lib/paymos/errors.rb,
sig/paymos.rbs
Direct Known Subclasses
AuthenticationError, ConflictError, GoneError, NotFoundError, RateLimitError, ServerError, ValidationError
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
Returns the value of attribute body.
-
#headers ⇒ Hash[String, String]
readonly
Returns the value of attribute headers.
-
#problem ⇒ Hash[String, untyped]?
readonly
Returns the value of attribute problem.
-
#status ⇒ Integer
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #code ⇒ String
- #detail ⇒ String
- #errors ⇒ Array[Hash[String, untyped]]
- #field ⇒ Object
-
#initialize(status, body, headers = {}) ⇒ ApiError
constructor
A new instance of ApiError.
- #retry_after_seconds(now: Time.now) ⇒ Integer?
Constructor Details
#initialize(status, body, headers = {}) ⇒ ApiError
Returns a new instance of ApiError.
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
#body ⇒ String (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/paymos/errors.rb', line 13 def body @body end |
#headers ⇒ Hash[String, String] (readonly)
Returns the value of attribute headers.
13 14 15 |
# File 'lib/paymos/errors.rb', line 13 def headers @headers end |
#problem ⇒ Hash[String, untyped]? (readonly)
Returns the value of attribute problem.
13 14 15 |
# File 'lib/paymos/errors.rb', line 13 def problem @problem end |
#status ⇒ Integer (readonly)
Returns the value of attribute status.
13 14 15 |
# File 'lib/paymos/errors.rb', line 13 def status @status end |
Instance Method Details
#code ⇒ 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 |
#detail ⇒ 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 |
#errors ⇒ 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 |
#field ⇒ 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?
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 |