forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_data.cpp
More file actions
155 lines (132 loc) · 4.59 KB
/
error_data.cpp
File metadata and controls
155 lines (132 loc) · 4.59 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
#include "duckdb/common/error_data.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/to_string.hpp"
#include "duckdb/common/types.hpp"
#include "duckdb/common/stacktrace.hpp"
#include "duckdb/parser/parsed_expression.hpp"
#include "duckdb/parser/query_error_context.hpp"
#include "duckdb/parser/tableref.hpp"
namespace duckdb {
ErrorData::ErrorData() : initialized(false), type(ExceptionType::INVALID) {
}
ErrorData::ErrorData(const std::exception &ex) : ErrorData(ex.what()) {
}
ErrorData::ErrorData(ExceptionType type, const string &message)
: initialized(true), type(type), raw_message(SanitizeErrorMessage(message)),
final_message(ConstructFinalMessage()) {
}
ErrorData::ErrorData(const string &message)
: initialized(true), type(ExceptionType::INVALID), raw_message(string()), final_message(string()) {
// parse the constructed JSON
if (message.empty() || message[0] != '{') {
// not JSON! Use the message as a raw Exception message and leave type as uninitialized
if (message == std::bad_alloc().what()) {
type = ExceptionType::OUT_OF_MEMORY;
raw_message = "Allocation failure";
} else {
raw_message = message;
}
} else {
auto info = StringUtil::ParseJSONMap(message)->Flatten();
for (auto &entry : info) {
if (entry.first == "exception_type") {
type = Exception::StringToExceptionType(entry.second);
} else if (entry.first == "exception_message") {
raw_message = SanitizeErrorMessage(entry.second);
} else {
extra_info[entry.first] = entry.second;
}
}
}
final_message = ConstructFinalMessage();
}
string ErrorData::SanitizeErrorMessage(string error) {
return StringUtil::Replace(std::move(error), string("\0", 1), "\\0");
}
string ErrorData::ConstructFinalMessage() const {
std::string error;
if (type != ExceptionType::UNKNOWN_TYPE) {
error = Exception::ExceptionTypeToString(type) + " ";
}
error += "Error: " + raw_message;
if (type == ExceptionType::INTERNAL) {
error += "\nThis error signals an assertion failure within DuckDB. This usually occurs due to "
"unexpected conditions or errors in the program's logic.\nFor more information, see "
"https://duckdb.org/docs/stable/dev/internal_errors";
// Ensure that we print the stack trace for internal exceptions.
auto entry = extra_info.find("stack_trace_pointers");
if (entry != extra_info.end()) {
auto stack_trace = StackTrace::ResolveStacktraceSymbols(entry->second);
error += "\n\nStack Trace:\n" + stack_trace;
}
}
return error;
}
void ErrorData::Throw(const string &prepended_message) const {
D_ASSERT(initialized);
if (!prepended_message.empty()) {
string new_message = prepended_message + raw_message;
throw Exception(type, new_message, extra_info);
} else {
throw Exception(type, raw_message, extra_info);
}
}
const ExceptionType &ErrorData::Type() const {
D_ASSERT(initialized);
return this->type;
}
bool ErrorData::operator==(const ErrorData &other) const {
if (initialized != other.initialized) {
return false;
}
if (type != other.type) {
return false;
}
return raw_message == other.raw_message;
}
void ErrorData::ConvertErrorToJSON() {
if (!raw_message.empty() && raw_message[0] == '{') {
// empty or already JSON
return;
}
raw_message = StringUtil::ExceptionToJSONMap(type, raw_message, extra_info);
final_message = raw_message;
}
void ErrorData::FinalizeError() {
auto entry = extra_info.find("stack_trace_pointers");
if (entry != extra_info.end()) {
auto stack_trace = StackTrace::ResolveStacktraceSymbols(entry->second);
extra_info["stack_trace"] = std::move(stack_trace);
extra_info.erase("stack_trace_pointers");
}
}
void ErrorData::AddErrorLocation(const string &query) {
if (!query.empty()) {
auto entry = extra_info.find("position");
if (entry != extra_info.end()) {
raw_message = QueryErrorContext::Format(query, raw_message, std::stoull(entry->second));
}
}
{
auto entry = extra_info.find("stack_trace");
if (entry != extra_info.end() && !entry->second.empty()) {
raw_message += "\n\nStack Trace:\n" + entry->second;
entry->second = "";
}
}
final_message = ConstructFinalMessage();
}
void ErrorData::AddQueryLocation(optional_idx query_location) {
Exception::SetQueryLocation(query_location, extra_info);
}
void ErrorData::AddQueryLocation(QueryErrorContext error_context) {
AddQueryLocation(error_context.query_location);
}
void ErrorData::AddQueryLocation(const ParsedExpression &ref) {
AddQueryLocation(ref.GetQueryLocation());
}
void ErrorData::AddQueryLocation(const TableRef &ref) {
AddQueryLocation(ref.query_location);
}
} // namespace duckdb