Skip to content

Commit 60a8902

Browse files
authored
Add expected Errors
2 parents df854e0 + ee1f252 commit 60a8902

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Sources/HTTPEngine/Errors.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Foundation
2+
3+
public struct Errors {
4+
enum Request: Error {
5+
case invalidURL
6+
}
7+
8+
enum Response: Error {
9+
case couldNotRetrieveStatusCode
10+
case unexpectedStatusCode(HTTPURLResponse)
11+
case redirect(Int)
12+
case unknown(Int)
13+
14+
enum ClientError: Error {
15+
case badRequest_400
16+
case invalidCredentials_401
17+
case forbidden_403
18+
case notFound_404
19+
case notAllowed_405
20+
case conflict_409
21+
case tooManyRequests_429
22+
case unkown(Int)
23+
}
24+
25+
enum ServerError: Error {
26+
case internalServerError_500
27+
case notImplemented_501
28+
case badGateway_502
29+
case unavailable_503
30+
case timeout_504
31+
case unkown(Int)
32+
}
33+
34+
static func errorWith(statusCode: Int) -> Error? {
35+
switch statusCode {
36+
case 200...299: return nil
37+
case 300...399: return Errors.Response.redirect(statusCode)
38+
case 400: return Errors.Response.ClientError.badRequest_400
39+
case 401: return Errors.Response.ClientError.invalidCredentials_401
40+
case 403: return Errors.Response.ClientError.forbidden_403
41+
case 404: return Errors.Response.ClientError.notFound_404
42+
case 405: return Errors.Response.ClientError.notAllowed_405
43+
case 409: return Errors.Response.ClientError.conflict_409
44+
case 429: return Errors.Response.ClientError.tooManyRequests_429
45+
case 402, 410...418, 430...499: return Errors.Response.ClientError.unkown(statusCode)
46+
case 500: return Errors.Response.ServerError.internalServerError_500
47+
case 501: return Errors.Response.ServerError.notImplemented_501
48+
case 502: return Errors.Response.ServerError.badGateway_502
49+
case 503: return Errors.Response.ServerError.unavailable_503
50+
case 504: return Errors.Response.ServerError.timeout_504
51+
case 505...599: return Errors.Response.ServerError.unkown(statusCode)
52+
default:
53+
return Errors.Response.unknown(statusCode)
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)