class MailerLite::Error

Base MailerLite error.

Public Class Methods

error_class(status) click to toggle source
# File lib/mailerlite/error.rb, line 24
def self.error_class(status)
  case status
  when 400 then MailerLite::BadRequest
  when 401 then MailerLite::Unauthorized
  when 404 then MailerLite::NotFound
  when 429 then MailerLite::TooManyRequests
  when 500 then MailerLite::InternalServerError
  end
end
error_message(response) click to toggle source

Returns the appropriate MailerLite error message based on response

@param response [Faraday::Env] HTTP response.

@return [String] MailerLite error message.

# File lib/mailerlite/error.rb, line 39
def self.error_message(response)
  return unless response.body.is_a?(Hash)
  return unless response.body['error']

  message = response.body['error']['message']
  MailerLite::Utils.presence(message)
end
from_response(response) click to toggle source

Returns the appropriate MailerLite::Error sublcass based on status and response message.

@param response [Faraday::Env] HTTP response.

@return [MailerLite::Error]

# File lib/mailerlite/error.rb, line 16
def self.from_response(response)
  status = response.status.to_i
  message = error_message(response)

  klass = error_class(status)
  klass&.new(message)
end
new(msg = nil) click to toggle source
# File lib/mailerlite/error.rb, line 6
def initialize(msg = nil)
  @message = msg
end