error_with_diagnostics.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_ERROR_WITH_DIAGNOSTICS_HPP
  8. #define BOOST_MYSQL_ERROR_WITH_DIAGNOSTICS_HPP
  9. #include <boost/mysql/diagnostics.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/system/system_error.hpp>
  12. namespace boost {
  13. namespace mysql {
  14. /**
  15. * \brief A system_error with an embedded diagnostics object.
  16. * \details
  17. * Like `boost::system::system_error`, but adds a \ref diagnostics member
  18. * containing additional information.
  19. */
  20. class error_with_diagnostics : public system::system_error
  21. {
  22. diagnostics diag_;
  23. static system::system_error create_base(const error_code& err, const diagnostics& diag)
  24. {
  25. return diag.client_message().empty() ? system::system_error(err)
  26. : system::system_error(err, diag.client_message());
  27. }
  28. public:
  29. /// Initializing constructor.
  30. error_with_diagnostics(const error_code& err, const diagnostics& diag)
  31. : system::system_error(create_base(err, diag)), diag_(diag)
  32. {
  33. }
  34. /**
  35. * \brief Retrieves the server diagnostics embedded in this object.
  36. * \par Exception safety
  37. * No-throw guarantee.
  38. *
  39. * \par Object lifetimes
  40. * The returned reference is valid as long as `*this` is alive.
  41. */
  42. const diagnostics& get_diagnostics() const noexcept { return diag_; }
  43. };
  44. } // namespace mysql
  45. } // namespace boost
  46. #endif