/src/mysql-server/libs/mysql/utils/error.h
Line | Count | Source |
1 | | // Copyright (c) 2023, 2025, Oracle and/or its affiliates. |
2 | | // |
3 | | // This program is free software; you can redistribute it and/or modify |
4 | | // it under the terms of the GNU General Public License, version 2.0, |
5 | | // as published by the Free Software Foundation. |
6 | | // |
7 | | // This program is designed to work with certain software (including |
8 | | // but not limited to OpenSSL) that is licensed under separate terms, |
9 | | // as designated in a particular file or component or in included license |
10 | | // documentation. The authors of MySQL hereby grant you an additional |
11 | | // permission to link the program and your derivative works with the |
12 | | // separately licensed software that they have either included with |
13 | | // the program or referenced in the documentation. |
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 02110-1301 USA. |
23 | | |
24 | | #ifndef MYSQL_UTILS_ERROR_H |
25 | | #define MYSQL_UTILS_ERROR_H |
26 | | |
27 | | /// @file |
28 | | /// Experimental API header |
29 | | |
30 | | #include <memory> |
31 | | #include <sstream> |
32 | | #include <string> |
33 | | |
34 | | /// @addtogroup GroupLibsMysqlUtils |
35 | | /// @{ |
36 | | |
37 | | namespace mysql::utils { |
38 | | |
39 | | class Error; |
40 | | |
41 | | using Error_ptr = std::unique_ptr<mysql::utils::Error>; |
42 | | |
43 | | /// @brief Error representation used internally in case final error code |
44 | | /// is unknown and error situation handling needs to be done by the caller |
45 | | class Error { |
46 | | public: |
47 | | Error() = default; |
48 | | |
49 | | /// @brief Constructor |
50 | | /// @param[in] type Information about error type |
51 | | /// @param[in] file File name in which error occurred |
52 | | /// @param[in] line Line number in which error occurred |
53 | | Error(const char *type, const char *file, std::size_t line) |
54 | 0 | : Error(type, file, line, "") {} |
55 | | |
56 | | /// @brief Constructor |
57 | | /// @param[in] type Information about error type |
58 | | /// @param[in] file File name in which error occurred |
59 | | /// @param[in] line Line number in which error occurred |
60 | | /// @param[in] message Additional information |
61 | | Error(const char *type, [[maybe_unused]] const char *file, |
62 | 0 | [[maybe_unused]] std::size_t line, const char *message) { |
63 | 0 | #ifndef NDEBUG |
64 | 0 | m_stream << type << " error occurred in file: " << file |
65 | 0 | << " line: " << line; |
66 | 0 | m_stream << "; Message: " << message; |
67 | 0 | #else |
68 | 0 | m_stream << type << ": " << message; |
69 | 0 | #endif |
70 | 0 | m_message = m_stream.str(); |
71 | 0 | m_user_message = message; |
72 | 0 | m_is_error = true; |
73 | 0 | } |
74 | | |
75 | | /// @brief Function that indicates whether error occurred |
76 | 0 | bool is_error() const { return m_is_error; } |
77 | | |
78 | | /// @brief Information about error |
79 | | /// @return Message const string |
80 | 0 | const char *what() const noexcept { return m_message.c_str(); } |
81 | | |
82 | | /// @brief Returns only message, no other information |
83 | | /// @return Message const string |
84 | 0 | const char *get_message() const noexcept { return m_user_message; } |
85 | | |
86 | | protected: |
87 | | std::stringstream m_stream; ///< Internal stream to build the message string |
88 | | std::string m_message; ///< Message ready to be displayed |
89 | | const char *m_user_message; ///< Only message |
90 | | bool m_is_error = false; ///< object state, "false" means "no error" |
91 | | }; |
92 | | |
93 | | } // namespace mysql::utils |
94 | | |
95 | | /// @} |
96 | | |
97 | | #endif // MYSQL_UTILS_ERROR_H |