-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestStateError.swift
More file actions
125 lines (113 loc) · 5.5 KB
/
Copy pathTestStateError.swift
File metadata and controls
125 lines (113 loc) · 5.5 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
import Foundation
import DataSourcerer
enum TestStateError : ResourceError, Codable {
case unknown(description: String?)
case unreachable
case notConnectedToInternet
case deserializationFailed(path: String?, debugDescription: String?, responseSize: Int)
case requestTagsChanged
case notAuthenticated
case cacheCouldNotLoad(StateErrorMessage)
var errorMessage: StateErrorMessage {
let defaultMessage = NSLocalizedString("An error occurred while loading.\nPlease try again!", comment: "")
let message: String = {
switch self {
case let .unknown(description):
return description ?? defaultMessage
case .unreachable:
return defaultMessage
case .deserializationFailed:
return NSLocalizedString("A critical error occurred while deserializing.", comment: "")
case .requestTagsChanged:
return NSLocalizedString("No data could be loaded for your user.", comment: "")
case .notAuthenticated:
return NSLocalizedString("Please sign in.", comment: "")
case .notConnectedToInternet:
return NSLocalizedString("Please check your internet connectivity.", comment: "")
case let .cacheCouldNotLoad(errorMessage):
switch errorMessage {
case .default: return "Stored data could not be loaded."
case let .message(message): return message
}
}
}()
return .message(message)
}
init(message: StateErrorMessage) {
self = .cacheCouldNotLoad(message)
}
}
extension TestStateError {
enum CodingKeys: String, CodingKey {
case enumCaseKey = "type"
case deserializationFailed
case deserializationFailedPath
case deserializationFailedDebugDescription
case deserializationFailedResponseSize
case unknown
case unknownDescription
case requestTagsChanged
case unreachable
case notAuthenticated
case notConnectedToInternet
case cacheCouldNotLoad
case cacheCouldNotLoadType
}
internal init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let enumCaseString = try container.decode(String.self, forKey: .enumCaseKey)
guard let enumCase = CodingKeys(rawValue: enumCaseString) else {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Unknown enum case '\(enumCaseString)'"))
}
switch enumCase {
case .deserializationFailed:
let path = try? container.decode(String.self, forKey: .deserializationFailedPath)
let debugDescription = try? container.decode(String.self, forKey: .deserializationFailedDebugDescription)
let responseSize = try? container.decode(Int.self, forKey: .deserializationFailedResponseSize)
self = .deserializationFailed(path: (path ?? "no path available"), debugDescription: debugDescription, responseSize: responseSize ?? 0)
case .unknown:
let unknownDecription = try? container.decode(String.self, forKey: .unknownDescription)
self = .unknown(description: unknownDecription)
case .notAuthenticated:
self = .notAuthenticated
case .requestTagsChanged:
self = .requestTagsChanged
case .unreachable:
self = .unreachable
case .notConnectedToInternet:
self = .notConnectedToInternet
case .cacheCouldNotLoad:
let type = try? container.decode(StateErrorMessage.self, forKey: .cacheCouldNotLoadType)
self = .cacheCouldNotLoad(type ?? .default)
default: throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Unknown enum case '\(enumCase)'"))
}
}
internal func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .deserializationFailed(path, debugDescription, responseSize):
try container.encode(CodingKeys.deserializationFailed.rawValue, forKey: .enumCaseKey)
if let path = path {
try container.encode(path, forKey: .deserializationFailedPath)
}
if let debugDescription = debugDescription {
try container.encode(debugDescription, forKey: .deserializationFailedDebugDescription)
}
try container.encode(responseSize, forKey: .deserializationFailedResponseSize)
case let .unknown(description):
try container.encode(CodingKeys.unknown.rawValue, forKey: .enumCaseKey)
try container.encode(description, forKey: .unknownDescription)
case .requestTagsChanged:
try container.encode(CodingKeys.requestTagsChanged.rawValue, forKey: .enumCaseKey)
case .unreachable:
try container.encode(CodingKeys.unreachable.rawValue, forKey: .enumCaseKey)
case .notAuthenticated:
try container.encode(CodingKeys.notAuthenticated.rawValue, forKey: .enumCaseKey)
case .notConnectedToInternet:
try container.encode(CodingKeys.notConnectedToInternet.rawValue, forKey: .enumCaseKey)
case let .cacheCouldNotLoad(type):
try container.encode(CodingKeys.cacheCouldNotLoad.rawValue, forKey: .enumCaseKey)
try container.encode(type, forKey: .cacheCouldNotLoadType)
}
}
}