forked from apple/foundationdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.cpp
238 lines (206 loc) · 7.47 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
/*
* Error.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include "flow/Error.h"
#include "flow/Knobs.h"
#include "flow/Trace.h"
#include "flow/UnitTest.h"
bool g_crashOnError = false;
#define DEBUG_ERROR 0
#if DEBUG_ERROR
std::set<int> debugErrorSet = std::set<int>{ error_code_platform_error };
#define SHOULD_LOG_ERROR(x) (debugErrorSet.count(x) > 0)
#endif
Error Error::fromUnvalidatedCode(int code) {
if (code < 0 || code > 30000) {
Error e = Error::fromCode(error_code_unknown_error);
TraceEvent(SevWarn, "ConvertedUnvalidatedErrorCode").error(e).detail("OriginalCode", code);
return e;
} else
return Error::fromCode(code);
}
bool Error::isDiskError() const {
return (error_code == error_code_io_error || error_code == error_code_io_timeout);
}
Error internal_error_impl(const char* file, int line) {
fprintf(stderr, "Internal Error @ %s %d:\n %s\n", file, line, platform::get_backtrace().c_str());
TraceEvent(SevError, "InternalError")
.error(Error::fromCode(error_code_internal_error))
.detail("File", file)
.detail("Line", line)
.setErrorKind(ErrorKind::BugDetected)
.backtrace();
flushTraceFileVoid();
return Error(error_code_internal_error);
}
Error internal_error_impl(const char* msg, const char* file, int line) {
fprintf(stderr, "Assertion %s failed @ %s %d:\n %s\n", msg, file, line, platform::get_backtrace().c_str());
TraceEvent(SevError, "InternalError")
.error(Error::fromCode(error_code_internal_error))
.detail("FailedAssertion", msg)
.detail("File", file)
.detail("Line", line)
.setErrorKind(ErrorKind::BugDetected)
.backtrace();
flushTraceFileVoid();
return Error(error_code_internal_error);
}
Error internal_error_impl(const char* a_nm,
std::string const& a,
const char* op_nm,
const char* b_nm,
std::string const& b,
const char* file,
int line) {
fprintf(stderr, "Assertion failed @ %s %d:\n", file, line);
fprintf(stderr, " expression:\n");
fprintf(stderr, " %s %s %s\n", a_nm, op_nm, b_nm);
fprintf(stderr, " expands to:\n");
fprintf(stderr, " %s %s %s\n\n", a.c_str(), op_nm, b.c_str());
fprintf(stderr, " %s\n", platform::get_backtrace().c_str());
TraceEvent(SevError, "InternalError")
.error(Error::fromCode(error_code_internal_error))
.detailf("FailedAssertion", "%s %s %s", a_nm, op_nm, b_nm)
.detail("LeftValue", a)
.detail("RightValue", b)
.detail("File", file)
.detail("Line", line)
.setErrorKind(ErrorKind::BugDetected)
.backtrace();
flushTraceFileVoid();
return Error(error_code_internal_error);
}
Error::Error() : error_code(invalid_error_code), flags(0) {}
Error::Error(int error_code) : error_code(error_code), flags(0) {
if (TRACE_SAMPLE())
TraceEvent(SevSample, "ErrorCreated").detail("ErrorCode", error_code);
if (error_code >= 3000 && error_code < 6000) {
{
TraceEvent te(SevError, "SystemError");
te.error(*this).backtrace();
if (error_code == error_code_unknown_error) {
auto exception = std::current_exception();
if (exception) {
try {
std::rethrow_exception(exception);
} catch (std::exception& e) {
te.detail("StdException", e.what());
} catch (...) {
}
}
}
}
if (g_crashOnError) {
flushOutputStreams();
flushTraceFileVoid();
crashAndDie();
}
}
#if DEBUG_ERROR
if (SHOULD_LOG_ERROR(error_code)) {
TraceEvent te(SevWarn, "DebugError");
te.error(*this).backtrace();
if (error_code == error_code_unknown_error) {
auto exception = std::current_exception();
if (exception) {
try {
std::rethrow_exception(exception);
} catch (std::exception& e) {
te.detail("StdException", e.what());
} catch (...) {
}
}
}
}
#endif
}
ErrorCodeTable& Error::errorCodeTable() {
static ErrorCodeTable table;
return table;
}
const char* Error::name() const {
const auto& table = errorCodeTable();
auto it = table.find(error_code);
if (it == table.end())
return "UNKNOWN_ERROR";
return it->second.first;
}
const char* Error::what() const {
const auto& table = errorCodeTable();
auto it = table.find(error_code);
if (it == table.end())
return "UNKNOWN_ERROR";
return it->second.second;
}
void Error::init() {
errorCodeTable();
}
Error Error::asInjectedFault() const {
Error e = *this;
e.flags |= FLAG_INJECTED_FAULT;
return e;
}
AttributeNotFoundError::AttributeNotFoundError(const std::string& missingAttribute_)
: Error(error_code_attribute_not_found), missingAttribute(missingAttribute_) {}
const std::string& AttributeNotFoundError::getMissingAttribute() const {
return missingAttribute;
}
ErrorCodeTable::ErrorCodeTable() {
#define ERROR(name, number, description) \
addCode(number, #name, description); \
enum { Duplicate_Error_Code_##number = 0 };
#include "flow/error_definitions.h"
}
void ErrorCodeTable::addCode(int code, const char* name, const char* description) {
(*this)[code] = std::make_pair(name, description);
}
bool isAssertDisabled(int line) {
return FLOW_KNOBS && (FLOW_KNOBS->DISABLE_ASSERTS == -1 || FLOW_KNOBS->DISABLE_ASSERTS == line);
}
void breakpoint_me() {
return;
}
// FIXME: combine with bindings/c/fdb_c.cpp fdb_error_predicate function
const std::set<int> transactionRetryableErrors = { error_code_not_committed,
error_code_transaction_too_old,
error_code_future_version,
error_code_commit_proxy_memory_limit_exceeded,
error_code_grv_proxy_memory_limit_exceeded,
error_code_process_behind,
error_code_batch_transaction_throttled,
error_code_tag_throttled,
error_code_proxy_tag_throttled,
// maybe committed error
error_code_cluster_version_changed,
error_code_commit_unknown_result };
TEST_CASE("/flow/AssertTest") {
// this is mostly checking bug for bug compatibility with the C integer / sign promotion rules.
ASSERT_LT(-1, 0);
ASSERT_EQ(0, 0u);
ASSERT_GT(1, -1);
ASSERT_EQ((int32_t)0xFFFFFFFF, (int32_t)-1);
// ASSERT_LT(-1, 0u); // fails: -1 is promoted to unsigned value in comparison.
// ASSERT(-1 < 0u); // also fails
int sz = 42;
size_t ln = 43;
ASSERT(sz < ln);
ASSERT_EQ(0xFFFFFFFF, (int32_t)-1);
return Void();
}