- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy patherror_code.h
149 lines (124 loc) · 4.35 KB
/
error_code.h
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
/*
* Copyright (c) 2015, 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0,
* as published by the Free Software Foundation.
*
* This program is designed to work with certain software (including
* but not limited to OpenSSL) that is licensed under separate terms,
* as designated in a particular file or component or in included license
* documentation. The authors of MySQL hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have either included with
* the program or referenced in the documentation.
*
* 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, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PLUGIN_X_SRC_NGS_ERROR_CODE_H_
#definePLUGIN_X_SRC_NGS_ERROR_CODE_H_
#include<stdio.h>
#include<cstdarg>
#include<string>
#include"my_compiler.h"// NOLINT(build/include_subdir)
#include"my_dbug.h"// NOLINT(build/include_subdir)
#include"my_sys.h"// NOLINT(build/include_subdir)
#include"mysqld_error.h"// NOLINT(build/include_subdir)
namespacengs {
structError_code {
staticconstint MAX_MESSAGE_LENGTH = 1024;
int error;
std::string message;
std::string sql_state;
enum Severity {
OK = 0,
ERROR = 1,
FATAL = 2,
} severity;
Error_code() : error(0), severity(OK) {}
Error_code(int e, const std::string &m, const std::string &state = "HY000",
Severity sev = ERROR)
: error(e), message(m), sql_state(state), severity(sev) {
if (e) {
DBUG_PRINT("info", ("Error_code: %s", m.c_str()));
}
}
Error_code(int e, const std::string &state, Severity sev, constchar *fmt,
va_list args) MY_ATTRIBUTE((format(printf, 5, 0)));
Error_code(const Error_code &o) { operator=(o); }
Error_code &operator=(const Error_code &o) {
if (this != &o) {
error = o.error;
message = o.message;
sql_state = o.sql_state;
severity = o.severity;
}
return *this;
}
operatorbool() const { return error != 0; }
};
inlineError_code::Error_code(int e, const std::string &state, Severity sev,
constchar *fmt, va_list args)
: error(e), sql_state(state), severity(sev) {
char buffer[MAX_MESSAGE_LENGTH];
vsnprintf(buffer, sizeof(buffer), fmt, args);
message = buffer;
if (e) {
DBUG_PRINT("info", ("Error_code: %s", message.c_str()));
}
}
inline Error_code Success(constchar *msg, ...)
MY_ATTRIBUTE((format(printf, 1, 2)));
inline Error_code Error(int e, constchar *msg, ...)
MY_ATTRIBUTE((format(printf, 2, 3)));
inline Error_code Fatal(int e, constchar *msg, ...)
MY_ATTRIBUTE((format(printf, 2, 3)));
inline Error_code Success(constchar *msg, ...) {
va_list ap;
va_start(ap, msg);
Error_code tmp(Error_code(0, "", Error_code::OK, msg, ap));
va_end(ap);
return tmp;
}
inline Error_code Success() { returnError_code(); }
inline Error_code SQLError(constint error_code, ...) {
va_list ap;
va_start(ap, error_code);
constauto format = my_get_err_msg(error_code);
Error_code tmp(error_code, "");
if (nullptr != format)
tmp = Error_code(error_code, "HY000", Error_code::ERROR, format, ap);
va_end(ap);
return tmp;
}
inline Error_code SQLError_access_denied() {
returnError_code(ER_ACCESS_DENIED_ERROR, "Invalid user or password");
}
inline Error_code Error(int e, constchar *msg, ...) {
va_list ap;
va_start(ap, msg);
Error_code tmp(Error_code(e, "HY000", Error_code::ERROR, msg, ap));
va_end(ap);
return tmp;
}
inline Error_code Fatal(int e, constchar *msg, ...) {
va_list ap;
va_start(ap, msg);
Error_code tmp(Error_code(e, "HY000", Error_code::FATAL, msg, ap));
va_end(ap);
return tmp;
}
inline Error_code Fatal(const Error_code &err) {
Error_code error(err);
error.severity = Error_code::FATAL;
return error;
}
} // namespace ngs
#endif// PLUGIN_X_SRC_NGS_ERROR_CODE_H_