diagnostics.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_DIAGNOSTICS_HPP
  8. #define BOOST_MYSQL_DIAGNOSTICS_HPP
  9. #include <boost/mysql/string_view.hpp>
  10. #include <boost/mysql/detail/access.hpp>
  11. #include <string>
  12. namespace boost {
  13. namespace mysql {
  14. /**
  15. * \brief Contains additional information about errors.
  16. * \details
  17. * This class is a container for additional diagnostics about an operation that
  18. * failed. It can contain server-generated messages (\ref server_message) or client-side messages
  19. * (\ref client_message). More members may be added in the future.
  20. */
  21. class diagnostics
  22. {
  23. public:
  24. /**
  25. * \brief Constructs a diagnostics object with empty error messages.
  26. * \par Exception safety
  27. * No-throw guarantee.
  28. */
  29. diagnostics() = default;
  30. /**
  31. * \brief Gets the client-generated error message.
  32. * \details
  33. * Contrary to \ref server_message, the client message never contains any string data
  34. * returned by the server, and is always ASCII-encoded. If you're using the static interface,
  35. * it may contain C++ type identifiers, too.
  36. *
  37. * \par Exception safety
  38. * No-throw guarantee.
  39. *
  40. * \par Object lifetimes
  41. * The returned view is valid as long as `*this` is alive, hasn't been assigned-to
  42. * or moved-from, and \ref clear hasn't been called. Moving `*this` invalidates the view.
  43. */
  44. string_view client_message() const noexcept
  45. {
  46. return impl_.is_server ? string_view() : string_view(impl_.msg);
  47. }
  48. /**
  49. * \brief Gets the server-generated error message.
  50. * \details
  51. * It's encoded according to `character_set_results` character set, which
  52. * usually matches the connection's character set. It may potentially contain user input.
  53. *
  54. * \par Exception safety
  55. * No-throw guarantee.
  56. *
  57. * \par Object lifetimes
  58. * The returned view is valid as long as `*this` is alive, hasn't been assigned-to
  59. * or moved-from, and \ref clear hasn't been called. Moving `*this` invalidates the view.
  60. */
  61. string_view server_message() const noexcept
  62. {
  63. return impl_.is_server ? string_view(impl_.msg) : string_view();
  64. }
  65. /**
  66. * \brief Clears the error messages.
  67. * \par Exception safety
  68. * No-throw guarantee.
  69. */
  70. void clear() noexcept
  71. {
  72. impl_.is_server = false;
  73. impl_.msg.clear();
  74. }
  75. private:
  76. #ifndef BOOST_MYSQL_DOXYGEN
  77. struct
  78. {
  79. bool is_server{};
  80. std::string msg;
  81. void assign_client(std::string from)
  82. {
  83. msg = std::move(from);
  84. is_server = false;
  85. }
  86. void assign_server(std::string from)
  87. {
  88. msg = std::move(from);
  89. is_server = true;
  90. }
  91. } impl_;
  92. friend bool operator==(const diagnostics& lhs, const diagnostics& rhs) noexcept;
  93. friend struct detail::access;
  94. #endif
  95. };
  96. /**
  97. * \relates diagnostics
  98. * \brief Compares two diagnostics objects.
  99. * \par Exception safety
  100. * No-throw guarantee.
  101. */
  102. inline bool operator==(const diagnostics& lhs, const diagnostics& rhs) noexcept
  103. {
  104. return lhs.impl_.is_server == rhs.impl_.is_server && lhs.impl_.msg == rhs.impl_.msg;
  105. }
  106. /**
  107. * \relates diagnostics
  108. * \brief Compares two diagnostics objects.
  109. * \par Exception safety
  110. * No-throw guarantee.
  111. */
  112. inline bool operator!=(const diagnostics& lhs, const diagnostics& rhs) noexcept { return !(lhs == rhs); }
  113. } // namespace mysql
  114. } // namespace boost
  115. #endif