forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathError.cpp
More file actions
executable file
·271 lines (232 loc) · 8.21 KB
/
Error.cpp
File metadata and controls
executable file
·271 lines (232 loc) · 8.21 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
#include "Error.hpp"
std::string NSLocalizedDescriptionKey = "NSLocalizedDescription";
std::string NSLocalizedFailureErrorKey = "NSLocalizedFailure";
std::string NSLocalizedFailureReasonErrorKey = "NSLocalizedFailureReason";
std::string NSLocalizedRecoverySuggestionErrorKey = "NSLocalizedRecoverySuggestion";
std::string NSUnderlyingErrorKey = "NSUnderlyingError";
std::string NSDebugDescriptionErrorKey = "NSDebugDescription";
std::string ALTLocalizedDescriptionKey = "ALTLocalizedDescription";
std::string ALTLocalizedFailureReasonErrorKey = "ALTLocalizedFailureReason";
std::string ALTLocalizedRecoverySuggestionErrorKey = "ALTLocalizedRecoverySuggestion";
std::string ALTDebugDescriptionErrorKey = "ALTDebugDescription";
extern std::wstring WideStringFromString(std::string string);
std::string AnyStringValue(std::any& any)
{
try
{
std::string string = std::any_cast<std::string>(any);
return string;
}
catch (std::bad_any_cast) {}
try
{
std::optional<std::string> string = std::any_cast<std::optional<std::string>>(any);
if (string.has_value())
{
return *string;
}
else
{
return "";
}
}
catch (std::bad_any_cast) {}
try
{
char* string = std::any_cast<char*>(any);
return string;
}
catch (std::bad_any_cast) {}
try
{
const char* string = std::any_cast<const char*>(any);
return string;
}
catch (std::bad_any_cast) {}
try
{
int number = std::any_cast<int>(any);
return std::to_string(number);
}
catch (std::bad_any_cast) {}
std::shared_ptr<Error> error = std::any_cast<std::shared_ptr<Error>>(any);
std::ostringstream oss;
oss << *error;
return oss.str();
}
web::json::value Error::serialized() const
{
web::json::value serializedError = web::json::value();
serializedError[L"errorDomain"] = web::json::value::string(WideStringFromString(this->domain()));
serializedError[L"errorCode"] = web::json::value::number(this->code());
auto rawUserInfo = this->userInfo();
web::json::value userInfo;
for (auto pair : rawUserInfo)
{
try
{
std::string string = AnyStringValue(pair.second);
userInfo[WideStringFromString(pair.first)] = web::json::value::string(WideStringFromString(string));
continue;
}
catch (std::bad_any_cast) {}
try
{
int integer = std::any_cast<int>(pair.second);
userInfo[WideStringFromString(pair.first)] = web::json::value::number(integer);
continue;
}
catch (std::bad_any_cast) {}
try
{
auto error = std::any_cast<std::shared_ptr<Error>>(pair.second);
userInfo[WideStringFromString(pair.first)] = error->serialized();
continue;
}
catch (std::bad_any_cast) {}
// TODO: Support std::vector<std::any> and std::map<std::string, std::any>
// We can get away with this for now because we don't store either in error user info (yet).
}
userInfo[WideStringFromString(ALTLocalizedDescriptionKey)] = web::json::value::string(WideStringFromString(this->localizedDescription()));
auto localizedFailureReason = this->localizedFailureReason();
if (localizedFailureReason.has_value())
{
userInfo[WideStringFromString(ALTLocalizedFailureReasonErrorKey)] = web::json::value::string(WideStringFromString(*localizedFailureReason));
}
auto localizedRecoverySuggestion = this->localizedRecoverySuggestion();
if (localizedRecoverySuggestion.has_value())
{
userInfo[WideStringFromString(ALTLocalizedRecoverySuggestionErrorKey)] = web::json::value::string(WideStringFromString(*localizedRecoverySuggestion));
}
auto debugDescription = this->localizedDebugDescription();
if (debugDescription.has_value())
{
userInfo[WideStringFromString(ALTDebugDescriptionErrorKey)] = web::json::value::string(WideStringFromString(*debugDescription));
}
web::json::value legacyUserInfo;
for (auto pair : rawUserInfo)
{
try
{
auto value = AnyStringValue(pair.second);
legacyUserInfo[WideStringFromString(pair.first)] = web::json::value::string(WideStringFromString(value));
}
catch (std::bad_any_cast)
{
// legacyUserInfo only supports string values, so ignore all non-string values.
}
}
serializedError[L"errorUserInfo"] = userInfo;
serializedError[L"userInfo"] = legacyUserInfo;
return serializedError;
}
std::string Error::formattedDetailedDescription() const
{
std::vector<std::string> preferredKeyOrder = {
NSDebugDescriptionErrorKey,
NSLocalizedDescriptionKey,
NSLocalizedFailureErrorKey,
NSLocalizedFailureReasonErrorKey,
NSLocalizedRecoverySuggestionErrorKey,
NSUnderlyingErrorKey,
};
auto userInfo = this->userInfo();
userInfo[NSLocalizedDescriptionKey] = this->localizedDescription();
auto localizedFailure = this->localizedFailure();
if (localizedFailure.has_value())
{
userInfo[NSLocalizedFailureErrorKey] = *localizedFailure;
}
auto localizedFailureReason = this->localizedFailureReason();
if (localizedFailureReason.has_value())
{
userInfo[NSLocalizedFailureReasonErrorKey] = *localizedFailureReason;
}
auto localizedRecoverySuggestion = this->localizedRecoverySuggestion();
if (localizedRecoverySuggestion.has_value())
{
userInfo[NSLocalizedRecoverySuggestionErrorKey] = *localizedRecoverySuggestion;
}
auto localizedDebugDescription = this->localizedDebugDescription();
if (localizedDebugDescription.has_value())
{
userInfo[NSDebugDescriptionErrorKey] = *localizedDebugDescription;
}
std::vector<std::pair<std::string, std::any>> sortedUserInfo;
for (auto& pair : userInfo)
{
sortedUserInfo.push_back(pair);
}
sort(sortedUserInfo.begin(), sortedUserInfo.end(), [preferredKeyOrder](auto& a, auto& b) {
auto indexA = find(preferredKeyOrder.begin(), preferredKeyOrder.end(), a.first);
auto indexB = find(preferredKeyOrder.begin(), preferredKeyOrder.end(), b.first);
if (indexA != preferredKeyOrder.end() && indexB != preferredKeyOrder.end())
{
return indexA < indexB;
}
else if (indexA != preferredKeyOrder.end() && indexB == preferredKeyOrder.end())
{
// indexA exists, indexB does not, so A should come first.
return true;
}
else if (indexA == preferredKeyOrder.end() && indexB != preferredKeyOrder.end())
{
// indexA does not exist, indexB does, so B should come first.
return false;
}
else
{
// both indexes are nil, so sort alphabetically.
return a.first < b.first;
}
});
std::string detailedDescription;
for (auto& pair : sortedUserInfo)
{
std::string value;
try
{
value = AnyStringValue(pair.second);
}
catch (std::bad_any_cast)
{
continue;
}
std::string keyName;
if (pair.first == NSDebugDescriptionErrorKey)
{
keyName = "Debug Description";
}
else if (pair.first == NSLocalizedDescriptionKey)
{
keyName = "Error Description";
}
else if (pair.first == NSLocalizedFailureErrorKey)
{
keyName = "Failure";
}
else if (pair.first == NSLocalizedFailureReasonErrorKey)
{
keyName = "Failure Reason";
}
else if (pair.first == NSLocalizedRecoverySuggestionErrorKey)
{
keyName = "Recovery Suggestion";
}
else if (pair.first == NSUnderlyingErrorKey)
{
keyName = "Underlying Error";
}
else
{
keyName = pair.first;
}
std::string string = keyName + "\n" + value;
if (detailedDescription.length() > 0)
{
detailedDescription += "\n\n";
}
detailedDescription += string;
}
return detailedDescription;
}