Class: Sendly::ErrorFactory

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

Overview

Convert API response to appropriate error

Class Method Summary collapse

Class Method Details

.from_response(status, body) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sendly/errors.rb', line 97

def self.from_response(status, body)
  message = body["message"] || body["error"] || "Unknown error"
  code = body["code"]
  details = body["details"]

  case status
  when 400, 422
    ValidationError.new(message, details: details)
  when 401
    AuthenticationError.new(message)
  when 402
    InsufficientCreditsError.new(message)
  when 404
    NotFoundError.new(message)
  when 429
    retry_after = body["retry_after"] || body["retryAfter"]
    RateLimitError.new(message, retry_after: retry_after)
  when 500..599
    ServerError.new(message, status_code: status)
  else
    APIError.new(message, status_code: status, code: code, details: details)
  end
end