MySQL 8.4.2
Source Code Documentation
error.h
Go to the documentation of this file.
1// Copyright (c) 2023, 2024, 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
37namespace mysql::utils {
38
39class Error;
40
41using 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
45class 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 : 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 [[maybe_unused]] std::size_t line, const char *message) {
63#ifndef NDEBUG
64 m_stream << type << " error occurred in file: " << file
65 << " line: " << line;
66 m_stream << "; Message: " << message;
67#else
68 m_stream << type << ": " << message;
69#endif
70 m_message = m_stream.str();
71 m_user_message = message;
72 m_is_error = true;
73 }
74
75 /// @brief Function that indicates whether error occurred
76 bool is_error() const { return m_is_error; }
77
78 /// @brief Information about error
79 /// @return Message const string
80 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 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
Class representing an error.
Definition: error.h:48
Error representation used internally in case final error code is unknown and error situation handling...
Definition: error.h:45
std::stringstream m_stream
Internal stream to build the message string.
Definition: error.h:87
const char * get_message() const noexcept
Returns only message, no other information.
Definition: error.h:84
Error(const char *type, const char *file, std::size_t line)
Constructor.
Definition: error.h:53
std::string m_message
Message ready to be displayed.
Definition: error.h:88
const char * what() const noexcept
Information about error.
Definition: error.h:80
const char * m_user_message
Only message.
Definition: error.h:89
Error(const char *type, const char *file, std::size_t line, const char *message)
Constructor.
Definition: error.h:61
bool is_error() const
Function that indicates whether error occurred.
Definition: error.h:76
bool m_is_error
object state, "false" means "no error"
Definition: error.h:90
Definition: os0file.h:89
Definition: gtid_format.h:46
std::unique_ptr< mysql::utils::Error > Error_ptr
Definition: error.h:41
required string type
Definition: replication_group_member_actions.proto:34