Module: Fopost::ErrorFactory
- Defined in:
- lib/fopost/errors.rb
Overview
Maps an HTTP status plus a decoded body onto the most specific error class.
Constant Summary collapse
- BY_STATUS =
{ 400 => ValidationError, 401 => AuthenticationError, 402 => PaymentRequiredError, 403 => PermissionDeniedError, 404 => NotFoundError, 422 => ValidationError, 429 => RateLimitError }.freeze
Class Method Summary collapse
Class Method Details
.build(status, body, retry_after: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/fopost/errors.rb', line 81 def self.build(status, body, retry_after: nil) code = nil = "HTTP #{status}" if body.is_a?(Hash) code = body['error'] if body['error'].is_a?(String) if body['message'].is_a?(String) && !body['message'].empty? = body['message'] elsif code = code end elsif body.is_a?(String) && !body.strip.empty? = body.strip end klass = BY_STATUS.fetch(status, Error) if klass == RateLimitError return RateLimitError.new(, status: status, code: code, body: body, retry_after: retry_after) end klass.new(, status: status, code: code, body: body) end |