forked from vadymmarkov/Malibu
/
NetworkError.swift
65 lines (55 loc) · 1.92 KB
/
NetworkError.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
import Foundation
public enum NetworkError: Error {
case invalidRequestURL
case missingContentType(response: Response)
case invalidParameter
case noDataInResponse
case noResponseReceived
case unacceptableStatusCode(statusCode: Int, response: Response)
case unacceptableContentType(contentType: String, response: Response)
case jsonArraySerializationFailed(response: Response)
case jsonDictionarySerializationFailed(response: Response)
case stringSerializationFailed(encoding: UInt, response: Response)
public var reason: String {
var text: String
switch self {
case .invalidRequestURL:
text = "Invalid request URL"
case .missingContentType:
text = "Response content type was missing"
case .invalidParameter:
text = "Parameter is not convertible to NSData"
case .noDataInResponse:
text = "No data in response"
case .noResponseReceived:
text = "No response received"
case .unacceptableStatusCode(let statusCode, _):
text = "Response status code \(statusCode) was unacceptable"
case .unacceptableContentType(let contentType, _):
text = "Response content type \(contentType) was unacceptable"
case .jsonArraySerializationFailed:
text = "No JSON array in response data"
case .jsonDictionarySerializationFailed:
text = "No JSON dictionary in response data"
case .stringSerializationFailed(let encoding, _):
text = "String could not be serialized with encoding: \(encoding)"
}
return NSLocalizedString(text, comment: "")
}
}
// MARK: - Hashable
extension NetworkError: Hashable {
public var hashValue: Int {
return reason.hashValue
}
}
// MARK: - Equatable
public func == (lhs: NetworkError, rhs: NetworkError) -> Bool {
return lhs.reason == rhs.reason
}
// MARK: - NSError
extension NSError {
var isOffline: Bool {
return Int32(code) == CFNetworkErrors.cfurlErrorNotConnectedToInternet.rawValue
}
}