Module: DPay::Internal::ErrorMapper
- Defined in:
- lib/dpay/internal/error_mapper.rb,
sig/dpay/internal/error_mapper.rbs
Constant Summary collapse
- DEFAULT_MESSAGE =
"Unexpected API error"
Class Method Summary collapse
- .error_class(status) ⇒ Object
- .extract_message(data) ⇒ Object
- .int_header(response, name) ⇒ Object
- .map(response) ⇒ Object
- .normalize_field_errors(errors) ⇒ Object
- .normalize_messages(messages) ⇒ Object
- .rate_limit(response, message) ⇒ Object
- .scalar?(value) ⇒ Boolean
Instance Method Summary collapse
- #self?.error_class ⇒ singleton(ApiError)
- #self?.extract_message ⇒ String
- #self?.int_header ⇒ Integer?
- #self?.map ⇒ ApiError
- #self?.normalize_field_errors ⇒ Hash[String, Array[String]]
- #self?.normalize_messages ⇒ Array[String]?
- #self?.rate_limit ⇒ RateLimitError
- #self?.scalar? ⇒ Boolean
Class Method Details
.error_class(status) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/dpay/internal/error_mapper.rb', line 26 def error_class(status) case status when 401 then AuthenticationError when 403 then AccessDeniedError when 404 then NotFoundError when 400, 422 then InvalidRequestError else status >= 500 ? ApiServerError : ApiError end end |
.extract_message(data) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/dpay/internal/error_mapper.rb', line 36 def (data) return data["message"] if data["message"].is_a?(String) return data["msg"] if data["msg"].is_a?(String) DEFAULT_MESSAGE end |
.int_header(response, name) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dpay/internal/error_mapper.rb', line 78 def int_header(response, name) value = response.header(name) return nil if value.nil? match = /\A\s*[+-]?\d+/.match(value.to_s) return 0 if match.nil? digits = match[0] digits.nil? ? 0 : digits.strip.to_i end |
.map(response) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dpay/internal/error_mapper.rb', line 10 def map(response) decoded = response.decode_json data = decoded.is_a?(Hash) ? decoded : {} = (data) return rate_limit(response, ) if response.status == 429 error_class(response.status).new( , response.status, data["errorcode"].is_a?(String) ? data["errorcode"] : nil, normalize_field_errors(data["errors"]), response.body ) end |
.normalize_field_errors(errors) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dpay/internal/error_mapper.rb', line 54 def normalize_field_errors(errors) pairs = case errors when Array then errors.each_with_index.map { |, index| [index, ] } when Hash then errors.to_a else return {} end pairs.each_with_object({}) do |(field, ), result| normalized = () result[PHP.strval(field)] = normalized unless normalized.nil? end end |
.normalize_messages(messages) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/dpay/internal/error_mapper.rb', line 67 def () case when String then [] when Array then .select { || scalar?() }.map { || PHP.strval() } end end |
.rate_limit(response, message) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dpay/internal/error_mapper.rb', line 43 def rate_limit(response, ) RateLimitError.new( , response.status, int_header(response, "Retry-After"), int_header(response, "X-RateLimit-Limit"), int_header(response, "X-RateLimit-Remaining"), response.body ) end |
.scalar?(value) ⇒ Boolean
74 75 76 |
# File 'lib/dpay/internal/error_mapper.rb', line 74 def scalar?(value) value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(TrueClass) || value.is_a?(FalseClass) end |
Instance Method Details
#self?.error_class ⇒ singleton(ApiError)
7 |
# File 'sig/dpay/internal/error_mapper.rbs', line 7
def self?.error_class: (Integer status) -> singleton(ApiError)
|
#self?.extract_message ⇒ String
8 |
# File 'sig/dpay/internal/error_mapper.rbs', line 8
def self?.extract_message: (Hash[String, untyped] data) -> String
|
#self?.int_header ⇒ Integer?
13 |
# File 'sig/dpay/internal/error_mapper.rbs', line 13
def self?.int_header: (HTTP::Response response, String name) -> Integer?
|
#self?.map ⇒ ApiError
6 |
# File 'sig/dpay/internal/error_mapper.rbs', line 6
def self?.map: (HTTP::Response response) -> ApiError
|
#self?.normalize_field_errors ⇒ Hash[String, Array[String]]
10 |
# File 'sig/dpay/internal/error_mapper.rbs', line 10
def self?.normalize_field_errors: (untyped errors) -> Hash[String, Array[String]]
|
#self?.normalize_messages ⇒ Array[String]?
11 |
# File 'sig/dpay/internal/error_mapper.rbs', line 11
def self?.normalize_messages: (untyped messages) -> Array[String]?
|
#self?.rate_limit ⇒ RateLimitError
9 |
# File 'sig/dpay/internal/error_mapper.rbs', line 9
def self?.rate_limit: (HTTP::Response response, String message) -> RateLimitError
|
#self?.scalar? ⇒ Boolean
12 |
# File 'sig/dpay/internal/error_mapper.rbs', line 12
def self?.scalar?: (untyped value) -> bool
|