error_categories.ipp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_IMPL_ERROR_CATEGORIES_IPP
  8. #define BOOST_MYSQL_IMPL_ERROR_CATEGORIES_IPP
  9. #pragma once
  10. #include <boost/mysql/client_errc.hpp>
  11. #include <boost/mysql/common_server_errc.hpp>
  12. #include <boost/mysql/error_categories.hpp>
  13. #include <boost/mysql/detail/config.hpp>
  14. #include <boost/mysql/impl/internal/error/server_error_to_string.hpp>
  15. namespace boost {
  16. namespace mysql {
  17. namespace detail {
  18. inline const char* error_to_string(client_errc error)
  19. {
  20. switch (error)
  21. {
  22. case client_errc::incomplete_message: return "An incomplete message was received from the server";
  23. case client_errc::extra_bytes: return "Unexpected extra bytes at the end of a message were received";
  24. case client_errc::sequence_number_mismatch: return "Mismatched sequence numbers";
  25. case client_errc::server_unsupported:
  26. return "The server does not support the minimum required capabilities to establish the "
  27. "connection";
  28. case client_errc::protocol_value_error:
  29. return "An unexpected value was found in a server-received message";
  30. case client_errc::unknown_auth_plugin:
  31. return "The user employs an authentication plugin not known to this library";
  32. case client_errc::auth_plugin_requires_ssl:
  33. return "The authentication plugin requires the connection to use SSL";
  34. case client_errc::wrong_num_params:
  35. return "The number of parameters passed to the prepared statement does not match the "
  36. "number of actual parameters";
  37. case client_errc::server_doesnt_support_ssl:
  38. return "The connection is configured to require SSL, but the server doesn't allow SSL connections. "
  39. "Configure SSL on your server or change your connection to not require SSL";
  40. case client_errc::metadata_check_failed:
  41. return "The static interface detected a type mismatch between your declared row type and what the "
  42. "server returned. Verify your type definitions.";
  43. case client_errc::num_resultsets_mismatch:
  44. return "The static interface detected a mismatch between the number of resultsets passed as template "
  45. "arguments to static_results<T1, T2...>/static_execution_state<T1, T2...> and the number of "
  46. "results returned by server";
  47. case client_errc::static_row_parsing_error:
  48. return "The static interface encountered an error when parsing a field into a C++ data structure.";
  49. case client_errc::row_type_mismatch:
  50. return "The StaticRow type passed to read_some_rows does not correspond to the resultset type being "
  51. "read";
  52. case client_errc::timeout: return "An operation controlled by Boost.MySQL timed out";
  53. case client_errc::cancelled: return "An operation controlled by Boost.MySQL was cancelled";
  54. case client_errc::pool_not_running:
  55. return "Getting a connection from a connection_pool failed because the pool is not running. Ensure "
  56. "that you're calling connection_pool::async_run.";
  57. case client_errc::invalid_encoding:
  58. return "A string passed to a formatting function contains a byte sequence that can't be decoded with "
  59. "the current character set.";
  60. case client_errc::unformattable_value:
  61. return "A formatting operation could not format one of its arguments.";
  62. case client_errc::format_string_invalid_syntax:
  63. return "A format string with invalid syntax was provided to a SQL formatting function.";
  64. case client_errc::format_string_invalid_encoding:
  65. return "A format string with an invalid byte sequence was provided to a SQL formatting function.";
  66. case client_errc::format_string_manual_auto_mix:
  67. return "A format string mixes manual (e.g. {0}) and automatic (e.g. {}) indexing.";
  68. case client_errc::format_string_invalid_specifier:
  69. return "The supplied format specifier is not supported by the type being formatted.";
  70. case client_errc::format_arg_not_found:
  71. return "A format argument referenced by a format string was not found. Check the number of format "
  72. "arguments passed and their names.";
  73. case client_errc::unknown_character_set:
  74. return "The character set used by the connection is not known by the client. Use set_character_set "
  75. "or async_set_character_set before invoking operations that require a known charset.";
  76. case client_errc::max_buffer_size_exceeded:
  77. return "An operation attempted to read or write a packet larger than the maximum buffer size. "
  78. "Try increasing any_connection_params::max_buffer_size.";
  79. default: return "<unknown MySQL client error>";
  80. }
  81. }
  82. inline const char* error_to_string(common_server_errc v)
  83. {
  84. const char* res = detail::common_error_to_string(static_cast<int>(v));
  85. return res ? res : "<unknown server error>";
  86. }
  87. class client_category final : public boost::system::error_category
  88. {
  89. public:
  90. const char* name() const noexcept final override { return "mysql.client"; }
  91. std::string message(int ev) const final override { return error_to_string(static_cast<client_errc>(ev)); }
  92. };
  93. class common_server_category final : public boost::system::error_category
  94. {
  95. public:
  96. const char* name() const noexcept final override { return "mysql.common-server"; }
  97. std::string message(int ev) const final override
  98. {
  99. return error_to_string(static_cast<common_server_errc>(ev));
  100. }
  101. };
  102. class mysql_server_category final : public boost::system::error_category
  103. {
  104. public:
  105. const char* name() const noexcept final override { return "mysql.mysql-server"; }
  106. std::string message(int ev) const final override { return detail::mysql_error_to_string(ev); }
  107. };
  108. class mariadb_server_category final : public boost::system::error_category
  109. {
  110. public:
  111. const char* name() const noexcept final override { return "mysql.mariadb-server"; }
  112. std::string message(int ev) const final override { return detail::mariadb_error_to_string(ev); }
  113. };
  114. // Optimization, so that static initialization happens only once (reduces C++11 thread-safe initialization
  115. // overhead)
  116. struct all_categories
  117. {
  118. client_category client;
  119. common_server_category common_server;
  120. mysql_server_category mysql_server;
  121. mariadb_server_category mariadb_server;
  122. static const all_categories& get() noexcept
  123. {
  124. static all_categories res;
  125. return res;
  126. }
  127. };
  128. } // namespace detail
  129. } // namespace mysql
  130. } // namespace boost
  131. const boost::system::error_category& boost::mysql::get_client_category() noexcept
  132. {
  133. return detail::all_categories::get().client;
  134. }
  135. const boost::system::error_category& boost::mysql::get_common_server_category() noexcept
  136. {
  137. return detail::all_categories::get().common_server;
  138. }
  139. const boost::system::error_category& boost::mysql::get_mysql_server_category() noexcept
  140. {
  141. return detail::all_categories::get().mysql_server;
  142. }
  143. const boost::system::error_category& boost::mysql::get_mariadb_server_category() noexcept
  144. {
  145. return detail::all_categories::get().mariadb_server;
  146. }
  147. #endif