1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 
27 #ifndef _NGS_ERROR_CODE_H_
28 #define _NGS_ERROR_CODE_H_
29 
30 #include <string>
31 
32 #ifndef NGS_STANDALONE
33 #include "mysqld_error.h"
34 #include "mysql/service_my_snprintf.h"
35 #else
36 #include <cstdio>
37 #define my_vsnprintf vsnprintf
38 #endif
39 
40 #include <string>
41 #include <cstdarg>
42 
43 #ifdef __GNUC__
44 #define MY_PRINTF_FORMAT(a1, a2) __attribute__ ((format (printf, a1, a2)))
45 #else
46 #define MY_PRINTF_FORMAT(a1, a2)
47 #endif
48 
49 namespace ngs
50 {
51   struct Error_code
52   {
53     static const int MAX_MESSAGE_LENGTH = 1024;
54 
55     int error;
56     std::string message;
57     std::string sql_state;
58     enum Severity
59     {
60       OK = 0,
61       ERROR = 1,
62       FATAL = 2,
63     } severity;
64 
Error_codeError_code65     Error_code() : error(0), severity(OK) {}
66     Error_code(int e, const std::string &m, const std::string &state = "HY000", Severity sev = ERROR)
errorError_code67     : error(e), message(m), sql_state(state), severity(sev) {}
68 
Error_codeError_code69     Error_code(int e, const std::string &state, Severity sev, const char *fmt, va_list args)
70     : error(e), sql_state(state), severity(sev)
71     {
72       char buffer[MAX_MESSAGE_LENGTH];
73       my_vsnprintf(buffer, sizeof(buffer), fmt, args);
74       message = buffer;
75     }
76 
Error_codeError_code77     Error_code(const Error_code &o)
78     {
79       operator=(o);
80     }
81 
82     Error_code &operator = (const Error_code &o)
83     {
84       if (this != &o)
85       {
86         error = o.error;
87         message = o.message;
88         sql_state = o.sql_state;
89         severity = o.severity;
90       }
91       return *this;
92     }
93 
94     operator bool () const
95     {
96       return error != 0;
97     }
98   };
99 
100   inline Error_code Success(const char *msg, ...) MY_PRINTF_FORMAT(1, 2);
101   inline Error_code SQLError(int e, const std::string &sqlstate, const char *msg, ...) MY_PRINTF_FORMAT(3, 4);
102   inline Error_code Error(int e, const char *msg, ...) MY_PRINTF_FORMAT(2, 3);
103   inline Error_code Fatal(int e, const char *msg, ...) MY_PRINTF_FORMAT(2, 3);
104 
Success(const char * msg,...)105   inline Error_code Success(const char *msg, ...)
106   {
107     va_list ap;
108     va_start(ap, msg);
109     Error_code tmp(Error_code(0, "", Error_code::OK, msg, ap));
110     va_end(ap);
111     return tmp;
112   }
113 
Success()114   inline Error_code Success()
115   {
116     return Error_code();
117   }
118 
SQLError(int e,const std::string & sqlstate,const char * msg,...)119   inline Error_code SQLError(int e, const std::string &sqlstate, const char *msg, ...)
120   {
121     va_list ap;
122     va_start(ap, msg);
123     Error_code tmp(Error_code(e, sqlstate, Error_code::ERROR, msg, ap));
124     va_end(ap);
125     return tmp;
126   }
127 
Error(int e,const char * msg,...)128   inline Error_code Error(int e, const char *msg, ...)
129   {
130     va_list ap;
131     va_start(ap, msg);
132     Error_code tmp(Error_code(e, "HY000", Error_code::ERROR, msg, ap));
133     va_end(ap);
134     return tmp;
135   }
136 
Fatal(int e,const char * msg,...)137   inline Error_code Fatal(int e, const char *msg, ...)
138   {
139     va_list ap;
140     va_start(ap, msg);
141     Error_code tmp(Error_code(e, "HY000", Error_code::FATAL, msg, ap));
142     va_end(ap);
143     return tmp;
144   }
145 
Fatal(const Error_code & err)146   inline Error_code Fatal(const Error_code &err)
147   {
148     Error_code error(err);
149     error.severity = Error_code::FATAL;
150     return error;
151   }
152 } // namespcae ngs
153 
154 
155 #endif // _NGS_ERROR_CODE_H_
156