Exception: MailerLite::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/mailerlite/error.rb

Overview

Base MailerLite error.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = nil) ⇒ Error

Returns a new instance of Error.



6
7
8
# File 'lib/mailerlite/error.rb', line 6

def initialize(msg = nil)
  @message = msg
end

Class Method Details

.error_class(status) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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) ⇒ String

Returns the appropriate MailerLite error message based on response

Parameters:

  • response (Faraday::Env)

    HTTP response.

Returns:

  • (String)

    MailerLite error message.



39
40
41
42
43
44
45
# 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) ⇒ MailerLite::Error

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

Parameters:

  • response (Faraday::Env)

    HTTP response.

Returns:



16
17
18
19
20
21
22
# 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