-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
error.cpp
293 lines (218 loc) · 5.92 KB
/
error.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
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <[email protected]>
//
// This file is part of p44utils.
//
// p44utils 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.
//
// p44utils 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 p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#include "error.hpp"
#include <string.h>
#include <errno.h>
#include "utils.hpp"
using namespace p44;
// MARK: - error base class
Error::Error(ErrorCode aErrorCode) :
errorCode(aErrorCode)
{
}
Error::Error(ErrorCode aErrorCode, const std::string &aErrorMessage) :
errorCode(aErrorCode),
errorMessage(aErrorMessage)
{
}
void Error::setFormattedMessage(const char *aFmt, va_list aArgs, bool aAppend)
{
// now make the string
textCache.clear();
string_format_v(errorMessage, aAppend, aFmt, aArgs);
}
void Error::prefixMessage(const char *aFmt, ...)
{
textCache.clear();
string s;
va_list args;
va_start(args, aFmt);
string_format_v(s, false, aFmt, args);
va_end(args);
errorMessage.insert(0, s);
}
ErrorPtr Error::withPrefix(const char *aFmt, ...)
{
textCache.clear();
string s;
va_list args;
va_start(args, aFmt);
string_format_v(s, false, aFmt, args);
va_end(args);
errorMessage.insert(0, s);
return ErrorPtr(this);
}
const char *Error::domain()
{
return "Error_baseClass";
}
const char *Error::getErrorDomain() const
{
return Error::domain();
}
const char *Error::getErrorMessage() const
{
return errorMessage.c_str();
}
string Error::errorCodeText() const
{
#if ENABLE_NAMED_ERRORS
string errorText = string_format("(%s", getErrorDomain());
const char *errName = errorName();
if (!errName) errName = getErrorCode()==0 ? "OK" : "NotOK";
// Note: errorName() returning empty string means error has no error codes to show, only domain
if (*errName) {
errorText += "::";
errorText += errName;
if (getErrorCode()!=0) {
string_format_append(errorText, "[%ld]", getErrorCode());
}
}
errorText += ')';
return errorText;
#else
return string_format("(%s:%ld)", getErrorDomain() , errorCode);
#endif
}
string Error::description() const
{
string errorText;
if (errorMessage.size()>0)
errorText = errorMessage;
else
errorText = "Error";
// Append domain and code to message text
errorText += " " + errorCodeText();
return errorText;
}
const char* Error::text()
{
if (textCache.empty()) {
textCache = description();
}
return textCache.c_str(); // is safe to return, as textCache lives as error object member
}
const char* Error::text(ErrorPtr aError)
{
if (!aError) return "<none>";
return aError->text();
}
bool Error::isError(const char *aDomain, ErrorCode aErrorCode) const
{
return aErrorCode==errorCode && (aDomain==NULL || isDomain(aDomain));
}
bool Error::isDomain(const char *aDomain) const
{
return strcmp(aDomain, getErrorDomain())==0;
}
bool Error::isError(ErrorPtr aError, const char *aDomain, ErrorCode aErrorCode)
{
if (!aError) return false;
return aError->isError(aDomain, aErrorCode);
}
bool Error::isDomain(ErrorPtr aError, const char *aDomain)
{
if (!aError) return false;
return aError->isDomain(aDomain);
}
ErrorPtr Error::ok(ErrorPtr aError)
{
if (aError) return aError; // whatever it is, return it
return ErrorPtr(new Error(OK, "OK")); // aError is NULL, return an explicit OK
}
// MARK: - system error
const char *SysError::domain()
{
return "System";
}
const char *SysError::getErrorDomain() const
{
return SysError::domain();
}
SysError::SysError(const char *aContextMessage) :
Error(errno, string(nonNullCStr(aContextMessage)).append(nonNullCStr(strerror(errno))))
{
}
SysError::SysError(int aErrNo, const char *aContextMessage) :
Error(aErrNo, string(nonNullCStr(aContextMessage)).append(nonNullCStr(strerror(aErrNo))))
{
}
ErrorPtr SysError::errNo(const char *aContextMessage)
{
if (errno==0)
return ErrorPtr(); // empty, no error
return ErrorPtr(new SysError(aContextMessage));
}
ErrorPtr SysError::err(int aErrNo, const char *aContextMessage)
{
if (aErrNo==0)
return ErrorPtr(); // empty, no error
return ErrorPtr(new SysError(aErrNo, aContextMessage));
}
ErrorPtr SysError::retErr(int aRet, const char *aContextMessage)
{
if (aRet>=0) return nullptr;
return SysError::errNo(aContextMessage);
}
#ifdef ESP_PLATFORM
// MARK: - ESP IDF error
const char *EspError::domain()
{
return "ESP32";
}
const char *EspError::getErrorDomain() const
{
return SysError::domain();
}
EspError::EspError(esp_err_t aEspError, const char *aContextMessage) :
Error(aEspError, string(nonNullCStr(aContextMessage)).append(nonNullCStr(esp_err_to_name(aEspError))))
{
}
ErrorPtr EspError::err(esp_err_t aEspError, const char *aContextMessage)
{
if (aEspError==ESP_OK)
return ErrorPtr(); // empty, no error
return ErrorPtr(new EspError(aEspError, aContextMessage));
}
#endif // ESP_PLATFORM
// MARK: - web error
ErrorPtr WebError::webErr(uint16_t aHTTPError, const char *aFmt, ... )
{
if (aHTTPError==0)
return ErrorPtr(); // empty, no error
Error *errP = new WebError(aHTTPError);
va_list args;
va_start(args, aFmt);
errP->setFormattedMessage(aFmt, args, false);
va_end(args);
return ErrorPtr(errP);
}
// MARK: - text error
ErrorPtr TextError::err(const char *aFmt, ...)
{
Error *errP = new TextError();
va_list args;
va_start(args, aFmt);
errP->setFormattedMessage(aFmt, args, false);
va_end(args);
return ErrorPtr(errP);
}