error_categories.ipp 6.9 KB

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