-
Notifications
You must be signed in to change notification settings - Fork 32
/
Network+URLSessionError.swift
79 lines (61 loc) · 1.74 KB
/
Network+URLSessionError.swift
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
import Foundation
#if canImport(AlicerceCore)
import AlicerceCore
#endif
extension Network {
public enum URLSessionError: Error {
public typealias APIError = Error
public typealias TotalRetriedDelay = Retry.Delay
case noRequest(Error)
case http(HTTP.StatusCode, APIError?, URLResponse)
case noData(URLResponse)
case url(URLError)
case badResponse(URLResponse?)
case retry(Retry.Error, Retry.State)
case cancelled
}
}
extension Network.URLSessionError {
public var response: URLResponse? {
switch self {
case .noRequest,
.url,
.cancelled:
return nil
case .http(_, _, let response),
.noData(let response):
return response
case .badResponse(let response):
return response
case .retry(_, let state):
return (state.errors.last as? Network.URLSessionError)?.response
}
}
public var lastError: Network.URLSessionError {
switch self {
case .noRequest,
.http,
.noData,
.url,
.badResponse,
.cancelled:
return self
case .retry(_, let state):
return state.errors.last as? Network.URLSessionError ?? self
}
}
public var statusCode: HTTP.StatusCode? {
switch self {
case .http(let statusCode, _, _):
return statusCode
case .noRequest,
.noData,
.url,
.badResponse,
.cancelled:
return nil
case .retry(_, let state):
return (state.errors.last as? Network.URLSessionError)?.statusCode
}
}
}