-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandler.cpp
More file actions
227 lines (199 loc) · 7.39 KB
/
Copy pathErrorHandler.cpp
File metadata and controls
227 lines (199 loc) · 7.39 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
#include "ErrorHandler.h"
AsyncWebSocket* ErrorHandler::ws = nullptr;
bool ErrorHandler::debugEnabled = false;
void ErrorHandler::initialize(AsyncWebSocket* websocket, bool debug) {
ws = websocket;
debugEnabled = debug;
}
void ErrorHandler::sendError(ErrorCode code, const String& message, const String& function,
const JsonObject& additionalData) {
if (!ws) return;
String response = createErrorResponse(code, message, function, additionalData);
ws->textAll(response);
if (debugEnabled) {
Serial.println("ERROR [" + getSeverityString(ErrorSeverity::ERROR) + "]: " + message);
Serial.println("Function: " + function + ", Code: " + String((int)code));
}
}
void ErrorHandler::sendError(const ErrorContext& context) {
if (!ws) return;
String response = createErrorResponse(context.code, context.message, context.function, context.additionalData);
ws->textAll(response);
logError(context);
}
String ErrorHandler::createErrorResponse(ErrorCode code, const String& message,
const String& function, const JsonObject& additionalData) {
StaticJsonDocument<512> doc;
doc["function"] = "error";
doc["error_code"] = (int)code;
doc["message"] = message;
doc["severity"] = getSeverityString(ErrorSeverity::ERROR);
if (function.length() > 0) {
doc["source_function"] = function;
}
if (!additionalData.isNull()) {
doc["details"] = additionalData;
}
String response;
serializeJson(doc, response);
return response;
}
void ErrorHandler::logError(const ErrorContext& context) {
if (!debugEnabled) return;
Serial.print("ERROR [");
Serial.print(getSeverityString(context.severity));
Serial.print("] ");
Serial.print(context.message);
Serial.print(" (Code: ");
Serial.print((int)context.code);
Serial.print(")");
if (context.function.length() > 0) {
Serial.print(" in ");
Serial.print(context.function);
}
if (context.line > 0) {
Serial.print(" at line ");
Serial.print(context.line);
}
Serial.println();
if (context.details.length() > 0) {
Serial.println("Details: " + context.details);
}
}
bool ErrorHandler::isSuccess(ErrorCode code) {
return code == ErrorCode::SUCCESS;
}
String ErrorHandler::getErrorMessage(ErrorCode code) {
switch (code) {
case ErrorCode::SUCCESS:
return "Success";
case ErrorCode::INVALID_PARAMETER:
return "Invalid parameter provided";
case ErrorCode::MISSING_PARAMETER:
return "Required parameter is missing";
case ErrorCode::INVALID_JSON_FORMAT:
return "Invalid JSON format";
case ErrorCode::UNKNOWN_COMMAND:
return "Unknown command";
case ErrorCode::INVALID_PIN_NUMBER:
return "Invalid pin number";
case ErrorCode::INVALID_PIN_MODE:
return "Invalid pin mode";
case ErrorCode::PIN_NOT_CONFIGURED_AS_OUTPUT:
return "Pin not configured as output";
case ErrorCode::PIN_NOT_CONFIGURED_AS_INPUT:
return "Pin not configured as input";
case ErrorCode::PIN_ALREADY_CONFIGURED:
return "Pin already configured";
case ErrorCode::I2C_DEVICE_NOT_FOUND:
return "I2C device not found";
case ErrorCode::I2C_READ_FAILED:
return "I2C read operation failed";
case ErrorCode::I2C_WRITE_FAILED:
return "I2C write operation failed";
case ErrorCode::I2C_NOT_INITIALIZED:
return "I2C not initialized";
case ErrorCode::I2C_INVALID_DEVICE_ADDRESS:
return "Invalid I2C device address";
case ErrorCode::I2C_INVALID_REGISTER:
return "Invalid I2C register address";
case ErrorCode::I2C_INVALID_BYTE_COUNT:
return "Invalid I2C byte count";
case ErrorCode::SPI_INVALID_BYTE_COUNT:
return "Invalid SPI byte count";
case ErrorCode::SPI_COMMAND_FAILED:
return "SPI command failed";
case ErrorCode::SPI_NOT_INITIALIZED:
return "SPI not initialized";
case ErrorCode::SPI_NOT_AVAILABLE:
return "SPI not available on this device";
case ErrorCode::UART_INVALID_BAUDRATE:
return "Invalid UART baudrate";
case ErrorCode::UART_MESSAGE_TOO_LONG:
return "UART message too long";
case ErrorCode::UART_NOT_AVAILABLE:
return "UART not available";
case ErrorCode::WIFI_CONNECTION_FAILED:
return "WiFi connection failed";
case ErrorCode::WIFI_CREDENTIALS_MISSING:
return "WiFi credentials missing";
case ErrorCode::WIFI_ALREADY_CONNECTED:
return "WiFi already connected";
case ErrorCode::WIFI_NOT_CONNECTED:
return "WiFi not connected";
case ErrorCode::WIFI_SCAN_ERROR:
return "WiFi scan failed";
case ErrorCode::MEMORY_ALLOCATION_FAILED:
return "Memory allocation failed";
case ErrorCode::BUFFER_OVERFLOW:
return "Buffer overflow";
case ErrorCode::SYSTEM_INITIALIZATION_FAILED:
return "System initialization failed";
case ErrorCode::HARDWARE_NOT_AVAILABLE:
return "Hardware not available";
case ErrorCode::TIMEOUT_ERROR:
return "Operation timed out";
default:
return "Unknown error";
}
}
String ErrorHandler::getSeverityString(ErrorSeverity severity) {
switch (severity) {
case ErrorSeverity::INFO:
return "INFO";
case ErrorSeverity::WARNING:
return "WARNING";
case ErrorSeverity::ERROR:
return "ERROR";
case ErrorSeverity::CRITICAL:
return "CRITICAL";
default:
return "UNKNOWN";
}
}
// Parameter validation functions
ErrorCode ErrorHandler::validatePinNumber(int pin) {
if (pin < 0 || pin >= MAX_PIN_ID) {
return ErrorCode::INVALID_PIN_NUMBER;
}
return ErrorCode::SUCCESS;
}
ErrorCode ErrorHandler::validatePinMode(int mode) {
if (mode < 0 || mode > 2) {
return ErrorCode::INVALID_PIN_MODE;
}
return ErrorCode::SUCCESS;
}
ErrorCode ErrorHandler::validateDeviceAddress(int address) {
if (address < 1 || address > 127) {
return ErrorCode::I2C_INVALID_DEVICE_ADDRESS;
}
return ErrorCode::SUCCESS;
}
ErrorCode ErrorHandler::validateI2CByteCount(int bytes) {
if (bytes < I2C_MIN_BYTES || bytes > I2C_MAX_BYTES) {
return ErrorCode::I2C_INVALID_BYTE_COUNT;
}
return ErrorCode::SUCCESS;
}
ErrorCode ErrorHandler::validateSPIByteCount(int bytes) {
if (bytes < SPI_MIN_BYTES || bytes > SPI_MAX_BYTES) {
return ErrorCode::SPI_INVALID_BYTE_COUNT;
}
return ErrorCode::SUCCESS;
}
ErrorCode ErrorHandler::validateBaudrate(int baudrate) {
int validBaudrates[] = {9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};
for (int i = 0; i < sizeof(validBaudrates) / sizeof(validBaudrates[0]); i++) {
if (baudrate == validBaudrates[i]) {
return ErrorCode::SUCCESS;
}
}
return ErrorCode::UART_INVALID_BAUDRATE;
}
ErrorCode ErrorHandler::validateSampleCount(int samples) {
if (samples < 1 || samples > 1024) {
return ErrorCode::INVALID_PARAMETER;
}
return ErrorCode::SUCCESS;
}