result.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
  2. *
  3. * Distributed under the Boost Software License, Version 1.0. (See
  4. * accompanying file LICENSE.txt)
  5. */
  6. #ifndef BOOST_REDIS_ADAPTER_RESULT_HPP
  7. #define BOOST_REDIS_ADAPTER_RESULT_HPP
  8. #include <boost/redis/detail/resp3_type_to_error.hpp>
  9. #include <boost/redis/error.hpp>
  10. #include <boost/redis/resp3/type.hpp>
  11. #include <boost/system/result.hpp>
  12. #include <string>
  13. namespace boost::redis::adapter {
  14. /// Stores any resp3 error.
  15. struct error {
  16. /// RESP3 error data type.
  17. resp3::type data_type = resp3::type::invalid;
  18. /// Diagnostic error message sent by Redis.
  19. std::string diagnostic;
  20. };
  21. /** @brief Compares two error objects for equality
  22. * @relates error
  23. *
  24. * @param a Left hand side error object.
  25. * @param b Right hand side error object.
  26. */
  27. inline bool operator==(error const& a, error const& b)
  28. {
  29. return a.data_type == b.data_type && a.diagnostic == b.diagnostic;
  30. }
  31. /** @brief Compares two error objects for difference
  32. * @relates error
  33. *
  34. * @param a Left hand side error object.
  35. * @param b Right hand side error object.
  36. */
  37. inline bool operator!=(error const& a, error const& b) { return !(a == b); }
  38. /// Stores response to individual Redis commands.
  39. template <class Value>
  40. using result = system::result<Value, error>;
  41. /**
  42. * @brief Allows using @ref error with `boost::system::result`.
  43. * @param e The error to throw.
  44. * @relates error
  45. */
  46. BOOST_NORETURN inline void throw_exception_from_error(error const& e, boost::source_location const&)
  47. {
  48. throw system::system_error(
  49. system::error_code(detail::resp3_type_to_error(e.data_type)),
  50. e.diagnostic);
  51. }
  52. } // namespace boost::redis::adapter
  53. #endif // BOOST_REDIS_ADAPTER_RESULT_HPP