-
Notifications
You must be signed in to change notification settings - Fork 1
/
ErrorsModule.cpp
349 lines (295 loc) · 14.2 KB
/
ErrorsModule.cpp
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "ErrorsModule.hpp"
#include "NetworkErrors.hpp"
#include "SignErrors.hpp"
#define SUCCESS(X) "\033[1;32m" X "\033[0m\n" // bold green
#define WARNING(X) "\033[1;33m" X "\033[0m\n" // bold yellow
#define HEADER(X) "\033[1;34m" X "\033[0m\n" // bold white
#define INFO(X) "\033[1;37m" X "\033[0m\n" // bold white
#define LIST_ITEM(X) " - " X "\n"
// mostly implementations for static members
namespace ErrorsModule
{
const std::string SEPERATOR = "-------------------------------\n";
const std::string CLOSING_SEPERATOR = "-------------------------------";
const std::string POSSIBLE_CAUSES = HEADER("Possible Causes:");
const std::string RECOMMENDED_ACTIONS = HEADER("Recommended Actions:");
// Network errors
const std::string NETWORK_ERROR_HEADER = INFO("Network Notice:");
// File errors
const std::string FILE_ERROR_HEADER = INFO("File Notice:");
const std::string FILE_ERROR_POSSIBLE_CAUSES =
POSSIBLE_CAUSES + LIST_ITEM("File permissions.") +
LIST_ITEM("Disk space.") + LIST_ITEM("Network connection.");
const std::string FILE_ERROR_RECOMMENDED_ACTIONS =
RECOMMENDED_ACTIONS + LIST_ITEM("Check file permissions.") +
LIST_ITEM("Check disk space.") + LIST_ITEM("Check network connection.") +
LIST_ITEM("Try again later.");
const std::string GENERAL_ERROR_HEADER = HEADER("General ERROR:");
const std::string GENERAL_ERROR_RECOMMENDED_ACTIONS =
std::string(HEADER("Recommended Actions:")) +
LIST_ITEM("Restart the application.") + LIST_ITEM("Try again later.");
const std::string GENERAL_ERROR_NOTE =
std::string(INFO("Note:")) +
LIST_ITEM(
"Frequent failures might indicate a bigger issue. "
"Investigate accordingly.");
const std::string GENERAL_ERROR_POSSIBLE_CAUSES =
POSSIBLE_CAUSES + LIST_ITEM("Unknown.");
const std::string UNKNOWN_ERROR_HEADER = HEADER("Unknown ERROR:");
const std::string UNKNOWN_ERROR_RECOMMENDED_ACTIONS =
std::string(HEADER("Recommended Actions:")) +
LIST_ITEM("Restart the application.") + LIST_ITEM("Try again later.");
const std::string UNKNOWN_ERROR_NOTE =
std::string(HEADER("Note:")) + LIST_ITEM(
"Frequent failures might "
"indicate a bigger issue. "
"Investigate accordingly.");
const std::string UNKNOWN_ERROR_POSSIBLE_CAUSES =
std::string(HEADER("Possible Causes:")) + LIST_ITEM("Unknown.");
const std::string EXTRA_INFORMATION = HEADER("Extra Information:");
std::map<SendStatus, std::string> SendPacketException::SendStatusMessages = {
{SendStatus::SUCCESS,
std::string(SUCCESS("Packet Sent Successfully:")) + SEPERATOR +
LIST_ITEM("Packet sent successfully.") + CLOSING_SEPERATOR},
{SendStatus::FAILURE_NETWORK,
std::string(WARNING("Network Notice:")) +
WARNING("Failed to send packet to server.") + POSSIBLE_CAUSES +
LIST_ITEM("Network issues.") + LIST_ITEM("Server down.") +
LIST_ITEM("Server timeout.") + LIST_ITEM("Sending took too long.") +
RECOMMENDED_ACTIONS + LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR},
{SendStatus::OVERSIZED_PACKET,
SEPERATOR + NETWORK_ERROR_HEADER +
LIST_ITEM("Packet size exceeds maximum allowed size.") +
POSSIBLE_CAUSES + LIST_ITEM("Malformed packet.") +
LIST_ITEM("Using debug tests.") + LIST_ITEM("Network issues.") +
RECOMMENDED_ACTIONS +
LIST_ITEM("Check packet size valid values (see PacketUtils module)") +
LIST_ITEM("Check that packet size field matches actual packet size.") +
LIST_ITEM("Try again later.") + CLOSING_SEPERATOR},
{SendStatus::UNDERSIZED_PACKET,
SEPERATOR + NETWORK_ERROR_HEADER +
LIST_ITEM("Packet size to small for sending.") + POSSIBLE_CAUSES +
LIST_ITEM("Malformed packet.") + LIST_ITEM("Network issues.") +
RECOMMENDED_ACTIONS + LIST_ITEM("Check packet size.") +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Try again later.") + GENERAL_ERROR_RECOMMENDED_ACTIONS +
GENERAL_ERROR_NOTE + CLOSING_SEPERATOR}};
const std::string NetworkException::getMessageForStatus(
NetworkErrorStatus status)
{
auto it = statusMessages.find(status);
if (it != statusMessages.end())
{
return it->second;
}
return "Unknown Status";
}
const std::string NetworkException::getFullMessage(NetworkErrorStatus status,
const std::string& extraInfo)
{
if (extraInfo.empty())
{
return getMessageForStatus(status);
}
std::string mainMessage = getMessageForStatus(status);
return "\n" + mainMessage + "\n" + EXTRA_INFORMATION + extraInfo + "\n" +
CLOSING_SEPERATOR;
}
const std::string NoMoreFilesException::predefinedMessage =
SEPERATOR + INFO("No Files Found") + FILE_ERROR_POSSIBLE_CAUSES + "\n" +
FILE_ERROR_RECOMMENDED_ACTIONS + SEPERATOR;
// Initialize static members
std::map<NetworkErrorStatus, std::string> NetworkException::statusMessages = {
{NetworkErrorStatus::CONNECTOPN_CLOSED,
SEPERATOR + NETWORK_ERROR_HEADER +
LIST_ITEM("Connection "
"closed by server.") +
POSSIBLE_CAUSES + LIST_ITEM("Server down.") +
LIST_ITEM("Network issues.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check server status.") +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Try again later.") + CLOSING_SEPERATOR},
{NetworkErrorStatus::CONNECTION_REFUSED,
SEPERATOR + NETWORK_ERROR_HEADER + INFO("Connection refused by server.") +
POSSIBLE_CAUSES + LIST_ITEM("Server down.") +
LIST_ITEM("Network issues.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check server status.") +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Try again later.") + CLOSING_SEPERATOR},
{NetworkErrorStatus::UNKNOWN_ERROR,
SEPERATOR + NETWORK_ERROR_HEADER + UNKNOWN_ERROR_POSSIBLE_CAUSES + "\n" +
UNKNOWN_ERROR_RECOMMENDED_ACTIONS + "\n" + UNKNOWN_ERROR_NOTE +
CLOSING_SEPERATOR},
{NetworkErrorStatus::SERVER_DOWN,
SEPERATOR + NETWORK_ERROR_HEADER + LIST_ITEM("Server is down.") +
POSSIBLE_CAUSES + LIST_ITEM("Server down.") +
LIST_ITEM("Network issues.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check server status.") +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Try again later.") + GENERAL_ERROR_RECOMMENDED_ACTIONS +
GENERAL_ERROR_NOTE + CLOSING_SEPERATOR},
{NetworkErrorStatus::MAX_RETRY_REACHED,
std::string(WARNING("Network Notice:")) +
WARNING("Failed to send packet to server after 3 retries.") +
POSSIBLE_CAUSES + LIST_ITEM("Network issues.") +
LIST_ITEM("Server down.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") +
LIST_ITEM("Make sure the server port is open and matches the "
"application's port.") +
LIST_ITEM("Check configuration file (transfer.info, port.info).") +
LIST_ITEM("Try again later.") + GENERAL_ERROR_RECOMMENDED_ACTIONS +
GENERAL_ERROR_NOTE + CLOSING_SEPERATOR},
{NetworkErrorStatus::SEND_PACKET_EXCEPTION,
SEPERATOR + NETWORK_ERROR_HEADER + INFO("Failed to send packet.") +
POSSIBLE_CAUSES + LIST_ITEM("Network issues.") +
LIST_ITEM("Server down.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR},
{NetworkErrorStatus::ENDPOINT_CREATION_ERROR,
SEPERATOR + NETWORK_ERROR_HEADER + WARNING("Failed to create endpoint.") +
POSSIBLE_CAUSES + LIST_ITEM("Network issues.") +
LIST_ITEM("Server down.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR},
{NetworkErrorStatus::IP_CONVERSION_ERROR,
SEPERATOR + NETWORK_ERROR_HEADER +
WARNING("Failed to convert IP address.") + POSSIBLE_CAUSES +
LIST_ITEM("Network issues.") + LIST_ITEM("Server down.") +
RECOMMENDED_ACTIONS + LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR},
{NetworkErrorStatus::SOCKET_CREATION_ERROR,
SEPERATOR + NETWORK_ERROR_HEADER + WARNING("Failed to create socket.") +
POSSIBLE_CAUSES + LIST_ITEM("Network issues.") +
LIST_ITEM("Server down.") + RECOMMENDED_ACTIONS +
LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR},
{NetworkErrorStatus::CONNECTION_ERROR,
SEPERATOR + NETWORK_ERROR_HEADER +
LIST_ITEM("Failed to connect to server.") + POSSIBLE_CAUSES +
LIST_ITEM("Network issues.") + LIST_ITEM("Server down.") +
RECOMMENDED_ACTIONS + LIST_ITEM("Check network connection.") +
LIST_ITEM("Check server status.") + LIST_ITEM("Try again later.") +
GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" + GENERAL_ERROR_NOTE +
CLOSING_SEPERATOR}};
const std::map<SignInStatus, std::string> SignInError::statusMessages = {
{SUCCESS_SIGN_IN,
std::string(SUCCESS("Sign-in Successful:")) + SEPERATOR +
LIST_ITEM("- You can now proceed with application functionality.") +
CLOSING_SEPERATOR},
{FAILURE_NETWORK_ERROR,
HEADER("Network Failure:") + SEPERATOR +
LIST_ITEM("Please ensure you have a stable internet connection.") +
LIST_ITEM("Check if your firewall is blocking the application.") +
CLOSING_SEPERATOR},
{FAILURE_INVALID_UUID,
HEADER("Invalid UUID:") + SEPERATOR +
LIST_ITEM("Ensure you have signed up successfully.") +
LIST_ITEM("Make sure your UUID is correct.") +
LIST_ITEM("Retry the operation.") + CLOSING_SEPERATOR},
{FAILURE_INVALID_ME_INFO,
std::string(HEADER("Invalid 'Me' Information:")) + SEPERATOR +
LIST_ITEM("Your personal information appears to be corrupted.") +
LIST_ITEM("Remove corrupted files and try again.") +
CLOSING_SEPERATOR},
{FAILURE_GENERAL, GENERAL_ERROR_HEADER + GENERAL_ERROR_POSSIBLE_CAUSES +
"\n" + GENERAL_ERROR_RECOMMENDED_ACTIONS + "\n" +
GENERAL_ERROR_NOTE + CLOSING_SEPERATOR},
{FAILURE_INVALID_AES, WARNING("Invalid AES Key:") + SEPERATOR +
LIST_ITEM("Your encryption key is invalid.") +
LIST_ITEM("Contact support immediately.") +
CLOSING_SEPERATOR},
{ALREADY_REGISTERED,
INFO("Already Registered:") + SEPERATOR +
LIST_ITEM("Prior registration detected.") +
LIST_ITEM(
"If you've succeeded before, sign in to receive your AES key.") +
LIST_ITEM("If you failed before, consider re-registering.") +
CLOSING_SEPERATOR},
{FAILURE_IN_AES_ENCRYPTION,
INFO("AES Encryption Failure:") + SEPERATOR +
LIST_ITEM("The AES encryption process failed.") +
LIST_ITEM("Make sure you're using a valid AES key.") +
LIST_ITEM("Contact support if the problem persists.") +
CLOSING_SEPERATOR},
{FAILURE_RSA_KEY_CREATION,
INFO("RSA Key Creation Failure:") + SEPERATOR +
LIST_ITEM("Unable to create RSA keys.") +
LIST_ITEM("Check your system's encryption libraries.") +
LIST_ITEM("Update or reinstall software if necessary.") +
LIST_ITEM("Contact support if the problem persists.") +
CLOSING_SEPERATOR},
{FAILURE_INFO_CREATION,
INFO("Me-Info Creation Failure:") + SEPERATOR +
LIST_ITEM("Unable to create or update the 'Me.info' file.") +
LIST_ITEM("Ensure you have adequate disk space and write "
"permissions.") +
LIST_ITEM("Update or reinstall software if necessary.") +
LIST_ITEM("Contact support if the problem persists.") +
CLOSING_SEPERATOR}};
SignInError::SignInError(SignInStatus status)
: std::runtime_error(getMessageForStatus(status)), status_(status)
{
}
SignInError::SignInError(const std::string& extraInfo, SignInStatus status)
: std::runtime_error(getMessageForStatus(status)),
status_(status),
extraInfo_(extraInfo)
{
}
SignInStatus SignInError::getStatus() const { return status_; }
const std::string SignInError::getMessageForStatus(SignInStatus status)
{
auto it = statusMessages.find(status);
if (it != statusMessages.end())
{
return it->second;
}
return "Unknown status.";
}
const std::string SignInError::getFullMessage(enum SignInStatus status,
const std::string& extraInfo)
{
if (extraInfo.empty())
{
return getMessageForStatus(status);
}
std::string newLine = "\n";
std::string mainMessage = getMessageForStatus(status);
return newLine + mainMessage + "\n" + EXTRA_INFORMATION + extraInfo + "\n" +
CLOSING_SEPERATOR;
}
// sendPacketException
const std::string SendPacketException::getMessageForStatus(SendStatus status)
{
auto it = SendStatusMessages.find(status);
if (it != SendStatusMessages.end())
{
return it->second;
}
return "Unknown status.";
}
const std::string SendPacketException::getFullMessage(
enum SendStatus status, const std::string& extraInfo)
{
if (extraInfo.empty())
{
return getMessageForStatus(status);
}
std::string newLine = "\n";
std::string mainMessage = getMessageForStatus(status);
return newLine + mainMessage + "\n" + EXTRA_INFORMATION + extraInfo + "\n" +
CLOSING_SEPERATOR;
}
} // namespace ErrorsModule