-
Notifications
You must be signed in to change notification settings - Fork 16
/
error.rb
87 lines (76 loc) · 2.21 KB
/
error.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
module MailerLite
# Base MailerLite error.
class Error < StandardError
def initialize(msg = nil)
@message = msg
end
# Returns the appropriate MailerLite::Error sublcass based on status and
# response message.
#
# @param response [Faraday::Env] HTTP response.
#
# @return [MailerLite::Error]
def self.from_response(response)
status = response.status.to_i
message = error_message(response)
klass = error_class(status)
klass&.new(message)
end
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
# Returns the appropriate MailerLite error message based on response
#
# @param response [Faraday::Env] HTTP response.
#
# @return [String] MailerLite error message.
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
end
# Raised when MailerLite returns a 400 HTTP status code
class BadRequest < Error
# Default error message.
def to_s
@message || 'Missing a required parameter or calling invalid method'
end
end
# Raised when MailerLite returns a 401 HTTP status code
class Unauthorized < Error
# Default error message.
def to_s
@message || 'Invalid API key provided'
end
end
# Raised when MailerLite returns a 404 HTTP status code
class NotFound < Error
# Default error message.
def to_s
@message || "Can't find requested items"
end
end
# Raised when MailerLite returns a 500 HTTP status code
class InternalServerError < Error
# Default error message.
def to_s
@message || 'The server encountered an unexpected condition'
end
end
# Raised when MailerLite returns a 429 HTTP status code
class TooManyRequests < Error
# Default error message.
def to_s
@message || 'Too Many Requests'
end
end
end