forked from mironal/TwitterAPIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterAPIKitError.swift
More file actions
287 lines (249 loc) · 8.85 KB
/
TwitterAPIKitError.swift
File metadata and controls
287 lines (249 loc) · 8.85 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import Foundation
public enum TwitterAPIKitError: Error {
case requestFailed(reason: RequestFailureReason)
public enum RequestFailureReason {
case invalidURL(url: String)
case invalidParameter(parameter: [String: Any], cause: String)
case cannotEncodeStringToData(string: String)
case jsonSerializationFailed(obj: Any)
}
case responseFailed(reason: ResponseFailureReason)
public enum ResponseFailureReason {
case invalidResponse(error: Error?)
case unacceptableStatusCode(statusCode: Int, error: TwitterAPIErrorResponse, rateLimit: TwitterRateLimit?)
}
case responseSerializeFailed(reason: ResponseSerializationFailureReason)
public enum ResponseSerializationFailureReason {
case jsonSerializationFailed(error: Error)
case jsonDecodeFailed(error: Error)
case cannotConvert(data: Data, toTypeName: String)
}
case uploadMediaFailed(reason: UploadMediaFailureReason)
public enum UploadMediaFailureReason {
case processingFailed(error: UploadMediaError)
}
case refreshOAuth20TokenFailed(reason: RefreshOAuth20TokenFailureReason)
public enum RefreshOAuth20TokenFailureReason {
case invalidAuthenticationMethod(TwitterAuthenticationMethod)
case refreshTokenIsMissing
}
case unkonwn(error: Error)
public init(error: Error) {
if let error = error as? TwitterAPIKitError {
self = error
} else {
self = .unkonwn(error: error)
}
}
}
extension TwitterAPIKitError {
public var isRequestFailed: Bool {
if case .requestFailed = self { return true }
return false
}
public var isResponseFailed: Bool {
if case .responseFailed = self { return true }
return false
}
public var isResponseSerializeFailed: Bool {
if case .responseSerializeFailed = self { return true }
return false
}
public var isUploadMediaFailed: Bool {
if case .uploadMediaFailed = self { return true }
return false
}
public var isRefreshOAuth20TokenFailed: Bool {
if case .refreshOAuth20TokenFailed = self { return true }
return false
}
public var isUnkonwn: Bool {
if case .unkonwn = self { return true }
return false
}
public var underlyingError: Error? {
switch self {
case .requestFailed(let reason):
return reason.underlyingError
case .responseFailed(let reason):
return reason.underlyingError
case .responseSerializeFailed(let reason):
return reason.underlyingError
case .uploadMediaFailed(let reason):
return reason.underlyingError
case .refreshOAuth20TokenFailed(let reason):
return reason.underlyingError
case .unkonwn(let error):
return error
}
}
public var isCancelled: Bool {
guard let error = underlyingError as? URLError else {
return false
}
return error.code == .cancelled
}
}
extension TwitterAPIKitError {
public struct UploadMediaError: Decodable, Error {
public let code: Int
public let name: String
public let message: String
public init(code: Int, name: String, message: String) {
self.code = code
self.name = name
self.message = message
}
}
}
extension TwitterAPIKitError: LocalizedError {
public var errorDescription: String? {
switch self {
case .requestFailed(let reason):
return reason.localizedDescription
case .responseFailed(let reason):
return reason.localizedDescription
case .responseSerializeFailed(let reason):
return reason.localizedDescription
case .uploadMediaFailed(let reason):
return reason.localizedDescription
case .refreshOAuth20TokenFailed(let reason):
return reason.localizedDescription
case .unkonwn(let error):
return error.localizedDescription
}
}
}
extension TwitterAPIKitError.UploadMediaError: LocalizedError {
public var errorDescription: String? {
return "\(name)[code:\(code)]: \(message)"
}
}
extension TwitterAPIKitError.RequestFailureReason {
public var localizedDescription: String {
switch self {
case .invalidURL(let url):
return "URL is not valid: \(url)"
case .invalidParameter(let parameter, let cause):
return "Parameter is not valid: \(parameter), cause: \(cause)"
case .cannotEncodeStringToData(let string):
return "Could not encode \"\(string)\""
case .jsonSerializationFailed(let obj):
return "JSON could not be serialized. May be invalid object \(String(describing: obj))"
}
}
public var underlyingError: Error? {
switch self {
case .invalidURL,
.invalidParameter,
.cannotEncodeStringToData,
.jsonSerializationFailed:
return nil
}
}
}
extension TwitterAPIKitError.ResponseFailureReason {
public var localizedDescription: String {
switch self {
case let .invalidResponse(error: error):
if let error = error {
return "Response is invalid: \(error.localizedDescription)"
}
return "Response is invalid"
case let .unacceptableStatusCode(statusCode, error: error, rateLimit: _):
return "Response status code was unacceptable: \(statusCode) with message: \(error.message)"
}
}
public var underlyingError: Error? {
switch self {
case let .invalidResponse(error: error):
return error
case .unacceptableStatusCode(statusCode: _, error: _, rateLimit: _):
return nil
}
}
/// A status code in the case of unacceptableStatusCode.
public var statusCode: Int? {
if case .unacceptableStatusCode(statusCode: let statusCode, error: _, rateLimit: _) = self {
return statusCode
}
return nil
}
/// A rate limit in the case of unacceptableStatusCode.
public var rateLimit: TwitterRateLimit? {
if case .unacceptableStatusCode(statusCode: _, error: _, rateLimit: let rateLimit) = self {
return rateLimit
}
return nil
}
}
extension TwitterAPIKitError.ResponseSerializationFailureReason {
public var localizedDescription: String {
switch self {
case .jsonSerializationFailed(let error):
return "Response could not be serialized because of error:\n\(error.localizedDescription)"
case .jsonDecodeFailed(let error):
return "Response could not be decoded because of error:\n\(error.localizedDescription)"
case .cannotConvert(data: _, let toTypeName):
return "Response could not convert to \"\(toTypeName)\""
}
}
public var underlyingError: Error? {
switch self {
case let .jsonSerializationFailed(error: error),
let .jsonDecodeFailed(error: error):
return error
case .cannotConvert:
return nil
}
}
}
extension TwitterAPIKitError.UploadMediaFailureReason {
public var localizedDescription: String {
switch self {
case .processingFailed(let error):
return error.message
}
}
public var underlyingError: Error? {
switch self {
case let .processingFailed(error: error):
return error
}
}
}
extension TwitterAPIKitError.RefreshOAuth20TokenFailureReason {
public var localizedDescription: String {
switch self {
case .invalidAuthenticationMethod(let method):
return
"Token refresh is possible only when TwitterAuthenticationMethod is .oauth20. You are currently \(method)."
case .refreshTokenIsMissing:
return "Refresh token is missing."
}
}
public var underlyingError: Error? {
switch self {
case .invalidAuthenticationMethod, .refreshTokenIsMissing:
return nil
}
}
}
extension TwitterAPIKitError {
public var requestFailureReason: RequestFailureReason? {
guard case .requestFailed(reason: let reason) = self else { return nil }
return reason
}
public var responseFailureReason: ResponseFailureReason? {
guard case .responseFailed(let reason) = self else { return nil }
return reason
}
public var responseSerializationFailureReason: ResponseSerializationFailureReason? {
guard case .responseSerializeFailed(let reason) = self else { return nil }
return reason
}
public var refreshOAuth20TokenFailureReason: RefreshOAuth20TokenFailureReason? {
guard case .refreshOAuth20TokenFailed(let reason) = self else { return nil }
return reason
}
}