-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.rb
More file actions
214 lines (189 loc) · 5.28 KB
/
Copy patherrors.rb
File metadata and controls
214 lines (189 loc) · 5.28 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# frozen_string_literal: true
module Lithic
module Errors
class Error < StandardError
# @!attribute cause
#
# @return [StandardError, nil]
end
class ConversionError < Lithic::Errors::Error
# @return [StandardError, nil]
def cause = @cause.nil? ? super : @cause
# @api private
#
# @param on [Class<StandardError>]
# @param method [Symbol]
# @param target [Object]
# @param value [Object]
# @param cause [StandardError, nil]
def initialize(on:, method:, target:, value:, cause: nil)
cls = on.name.split("::").last
message = [
"Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
"To get the unparsed API response, use #{cls}[#{method.inspect}].",
cause && "Cause: #{cause.message}"
].filter(&:itself).join(" ")
@cause = cause
super(message)
end
end
class APIError < Lithic::Errors::Error
# @return [URI::Generic]
attr_accessor :url
# @return [Integer, nil]
attr_accessor :status
# @return [Object, nil]
attr_accessor :body
# @api private
#
# @param url [URI::Generic]
# @param status [Integer, nil]
# @param body [Object, nil]
# @param request [nil]
# @param response [nil]
# @param message [String, nil]
def initialize(url:, status: nil, body: nil, request: nil, response: nil, message: nil)
@url = url
@status = status
@body = body
@request = request
@response = response
super(message)
end
end
class APIConnectionError < Lithic::Errors::APIError
# @!attribute status
#
# @return [nil]
# @!attribute body
#
# @return [nil]
# @api private
#
# @param url [URI::Generic]
# @param status [nil]
# @param body [nil]
# @param request [nil]
# @param response [nil]
# @param message [String, nil]
def initialize(
url:,
status: nil,
body: nil,
request: nil,
response: nil,
message: "Connection error."
)
super
end
end
class APITimeoutError < Lithic::Errors::APIConnectionError
# @api private
#
# @param url [URI::Generic]
# @param status [nil]
# @param body [nil]
# @param request [nil]
# @param response [nil]
# @param message [String, nil]
def initialize(
url:,
status: nil,
body: nil,
request: nil,
response: nil,
message: "Request timed out."
)
super
end
end
class APIStatusError < Lithic::Errors::APIError
# @api private
#
# @param url [URI::Generic]
# @param status [Integer]
# @param body [Object, nil]
# @param request [nil]
# @param response [nil]
# @param message [String, nil]
#
# @return [self]
def self.for(url:, status:, body:, request:, response:, message: nil)
kwargs = {
url: url,
status: status,
body: body,
request: request,
response: response,
message: message
}
case status
in 400
Lithic::Errors::BadRequestError.new(**kwargs)
in 401
Lithic::Errors::AuthenticationError.new(**kwargs)
in 403
Lithic::Errors::PermissionDeniedError.new(**kwargs)
in 404
Lithic::Errors::NotFoundError.new(**kwargs)
in 409
Lithic::Errors::ConflictError.new(**kwargs)
in 422
Lithic::Errors::UnprocessableEntityError.new(**kwargs)
in 429
Lithic::Errors::RateLimitError.new(**kwargs)
in (500..)
Lithic::Errors::InternalServerError.new(**kwargs)
else
Lithic::Errors::APIStatusError.new(**kwargs)
end
end
# @!parse
# # @return [Integer]
# attr_accessor :status
# @api private
#
# @param url [URI::Generic]
# @param status [Integer]
# @param body [Object, nil]
# @param request [nil]
# @param response [nil]
# @param message [String, nil]
def initialize(url:, status:, body:, request:, response:, message: nil)
message ||= {url: url.to_s, status: status, body: body}
super(
url: url,
status: status,
body: body,
request: request,
response: response,
message: message&.to_s
)
end
end
class BadRequestError < Lithic::Errors::APIStatusError
HTTP_STATUS = 400
end
class AuthenticationError < Lithic::Errors::APIStatusError
HTTP_STATUS = 401
end
class PermissionDeniedError < Lithic::Errors::APIStatusError
HTTP_STATUS = 403
end
class NotFoundError < Lithic::Errors::APIStatusError
HTTP_STATUS = 404
end
class ConflictError < Lithic::Errors::APIStatusError
HTTP_STATUS = 409
end
class UnprocessableEntityError < Lithic::Errors::APIStatusError
HTTP_STATUS = 422
end
class RateLimitError < Lithic::Errors::APIStatusError
HTTP_STATUS = 429
end
class InternalServerError < Lithic::Errors::APIStatusError
HTTP_STATUS = (500..)
end
end
end