...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
This section describes the different error handling strategies you may use with this library, as well as the different overloads available for each function involving network transfers.
This library uses Boost.System error codes and exceptions, like Asio and Beast. Some server-reported errors may include additional diagnostics information. For example, if you issue a query and one of the referenced fields does not exist, the server will return an error message indicating which was the offending field. This library makes these diagnostics available through the following classes and functions:
diagnostics
An object containing this extra diagnostic information about an error.
diagnostics::server_message
contains the server-generated error string, if any.
error_with_diagnostics
An exception that inherits from boost::system::system_error
that contains a diagnostics
object.
throw_on_error
A utility function to check an error_code
and throw an error_with_diagnostics
if the operation failed.
is_fatal_error
Checks whether an error_code
is fatal and thus requires
re-establishing the connection.
Every piece of functionality involving network transfers is offered in four versions:
error_with_diagnostics
exception.
error_code
and diagnostics
.
These functions output an error_code
and a diagnostics
object
by lvalue reference to report failures.
diagnostics
.
When they fail, they call the completion handler with a non-empty error_code
.
diagnostics
.
They have a diagnostics&
parameter before the CompletionToken
. When they fail, they
set the diagnostics
parameter
to any server-provided diagnostic information, if available, and then call
the completion handler with a non-empty error_code
.
This library defines the following types of errors:
Type of error |
Values contained in... |
Error category |
Description |
---|---|---|---|
Client errors |
|
Failures detected by Boost.MySQL, like corrupt messages. |
|
Common server errors |
|
Errors reported by the server, common to both MySQL and MariaDB. No new codes will be added here, since the two DBs are currently developed independently. |
|
MySQL-specific server errors |
Integer codes in |
Errors reported by the server, specific to MySQL. New codes will be added in the future. |
|
MariaDB-specific server errors |
Integer codes in |
Errors reported by the server, specific to MariaDB. New codes will be added in the future. |
Note that new codes are added frequently, so server-specific codes are represented as integers, instead of enums.
When an operation on a established connection (like a query execution) results in an error, two situations may happen:
common_server_errc::er_no_such_table
because you misspelled a table name, it is safe to run other queries after
the failed one. Such errors are called non-fatal.
You can use is_fatal_error
to distinguish between fatal and non-fatal error codes.
The error message given by diagnostics::server_message
may contain user-provided input, and should be treated
as untrusted. For certain errors, the MySQL server will include
the offending field names and values, which may contain arbitrary input. Please
use with caution.
This message may contain non-ASCII characters. It's encoded using the connection's character set.
Some functions, like basic_format_context::get
,
use boost::system::result<T>
to communicate errors. result<T>
contains either a value (an instance of T
),
or an error_code
,
if the operation failed. result<T>
is similar to std::expected
, but only requires C++11.
Given a result<T>
object
r
, you can get its contained
value calling r.value()
.
If r
contained an error, a
boost::system::result
exception with the contained error
code is thrown. r.has_value()
,
r.has_error()
and r.error()
can
be used to inspect the object.