-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.h
More file actions
134 lines (117 loc) · 4.5 KB
/
errors.h
File metadata and controls
134 lines (117 loc) · 4.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
126
127
128
129
130
131
132
133
/* Copyright (c) 2023 Taha
* this program is free software: you can redistribute it and/or modify
* it under the terms of the gnu general public license as published by
* the free software foundation, either version 3 of the license, or
* (at your option) any later version.
* this program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose. see the
* gnu general public license for more details.
* you should have received a copy of the gnu general public license
* along with this program. if not, see <https://www.gnu.org/licenses/>.
*
* Author: Taha
* Date: 2023, Dec 9
* Description: For custom errors. Mainly for when the cryptographic protocol picked doesn't exist.
*/
#ifndef ERRORS_H
#define ERRORS_H
#include <fstream>
#include <functional>
#include <ctime>
#include <chrono>
#include <filesystem>
// error codes
enum ERRORS
{
NO_ERROR,
WRONG_TYPE_ERROR,
ELLIPTIC_CURVE_NOT_FOUND,
COMMUNICATION_PROTOCOL_NOT_FOUND,
HASHING_ALGORITHM_NOT_FOUND,
ENCRYPTION_ALGORITHM_NOT_FOUND,
VERIFICATION_ALGORITHM_NOT_FOUND,
NO_ONE_SENDING,
NOT_VERIFIED,
NO_PROTOCOL,
};
// error names as string
const constexpr static char* ERROR_STRING[]
{
"NO_ERROR",
"WRONG_TYPE_ERROR",
"ELLIPTIC_CURVE_NOT_FOUND",
"COMMUNICATION_PROTOCOL_NOT_FOUND",
"HASHING_ALGORITHM_NOT_FOUND",
"ENCRYPTION_ALGORITHM_NOT_FOUND",
"VERIFICATION_ALGORITHM_NOT_FOUND",
"NO_ONE_SENDING",
"NOT_VERIFIED",
"NO_PROTOCOL",
};
inline std::string abs_path(std::string file) {
return std::filesystem::absolute(file).string();
}
// if USE_DEFAULT_VALUES, when an algorithm is not found, it will use a predefined one
#define USE_DEFAULT_VALUES false
#define DEBUG_MODE true // debug mode will allow throwing errors rather than assigning them
#define ERRORS_LOG_FILE abs_path("log/errors.log")
#define NETWORK_LOG_FILE abs_path("log/network.log")
inline bool log_network_issues = true; // changable
class ErrorHandling
{
public:
ERRORS error = NO_ERROR;
ErrorHandling() = default;
// lambda function is for what to do in case of error
void error_handle(ERRORS check_error, auto&& lambda_for_error, auto&& lambda_for_unexpected_error, auto &&get_time)
{
// if error caused: can only be ENCRYPTION_ALGORITHM_NOT_FOUND
if(error != NO_ERROR) {
if(error == check_error) {
lambda_for_error();
} else {
// if a different error raised: unexpected behaviour
lambda_for_unexpected_error(error, get_time());
}
error = NO_ERROR; // assign error code to None
}
}
// CIPHER
// find error and raise it after adding to a log file
std::function<void(ERRORS, std::string)> encryption_unexpected_error=[](ERRORS error_code, std::string time) {
std::ofstream file(ERRORS_LOG_FILE, std::fstream::app);
file << "\nENCRYPTION UNEXPECTED ERROR (in Cryptography::ProtocolData::init_cipher_data) = TIME: "
<< time << "\tERROR_CODE: " << error_code << "\tERROR_ID: " << ERROR_STRING[error_code];
file.close();
throw error_code;
};
// VERIFICATION
// find error and raise it after adding to a log file
std::function<void(ERRORS, std::string)> verification_unexpected_error=[](ERRORS error_code, std::string time) {
std::ofstream file(ERRORS_LOG_FILE, std::fstream::app);
file << "\nVERIFICATION ALGORITHM ERROR (in Cryptography::ProtocolData::init) = TIME: "
<< time << "\tERROR_CODE: " << error_code << "\tERROR_ID: " << ERROR_STRING[error_code];
file.close();
throw error_code;
};
// ELLIPTIC CURVE
// find error and raise it after adding to a log file
std::function<void(ERRORS, std::string)> curve_unexpected_error=[](ERRORS error_code, std::string time) {
std::ofstream file(ERRORS_LOG_FILE, std::fstream::app);
file << "\nELLIPTIC CURVE UNEXPECTED ERROR (in Cryptography::ProtocolData::init_cipher_data) = TIME: "
<< time << "\tERROR_CODE: " << error_code << "\tERROR_ID: " << ERROR_STRING[error_code];
file.close();
throw error_code;
};
// HASHING
// find error and raise it after adding to a log file
std::function<void(ERRORS, std::string)> hashing_unexpected_error=[](ERRORS error_code, std::string time) {
std::ofstream file(ERRORS_LOG_FILE, std::fstream::app);
file << "\nHASHING UNEXPECTED ERROR (in Cryptography::ProtocolData::init_hash_data) = TIME: "
<< time << "\tERROR_CODE: " << error_code << "\tERROR_ID: " << ERROR_STRING[error_code];
file.close();
throw error_code;
};
};
#endif /* ERRORS_H */