result.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (c) 2018-2023 Marcelo Zimbres Silva ([email protected])
  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/resp3/type.hpp>
  9. #include <boost/redis/error.hpp>
  10. #include <boost/system/result.hpp>
  11. #include <string>
  12. namespace boost::redis::adapter
  13. {
  14. /** @brief Stores any resp3 error
  15. * @ingroup high-level-api
  16. */
  17. struct error {
  18. /// RESP3 error data type.
  19. resp3::type data_type = resp3::type::invalid;
  20. /// Diagnostic error message sent by Redis.
  21. std::string diagnostic;
  22. };
  23. /** @brief Compares two error objects for equality
  24. * @relates error
  25. *
  26. * @param a Left hand side error object.
  27. * @param b Right hand side error object.
  28. */
  29. inline bool operator==(error const& a, error const& b)
  30. {
  31. return a.data_type == b.data_type && a.diagnostic == b.diagnostic;
  32. }
  33. /** @brief Compares two error objects for difference
  34. * @relates error
  35. *
  36. * @param a Left hand side error object.
  37. * @param b Right hand side error object.
  38. */
  39. inline bool operator!=(error const& a, error const& b)
  40. {
  41. return !(a == b);
  42. }
  43. /** @brief Stores response to individual Redis commands
  44. * @ingroup high-level-api
  45. */
  46. template <class Value>
  47. using result = system::result<Value, error>;
  48. BOOST_NORETURN inline void
  49. throw_exception_from_error(error const & e, boost::source_location const &)
  50. {
  51. system::error_code ec;
  52. switch (e.data_type) {
  53. case resp3::type::simple_error:
  54. ec = redis::error::resp3_simple_error;
  55. break;
  56. case resp3::type::blob_error:
  57. ec = redis::error::resp3_blob_error;
  58. break;
  59. case resp3::type::null:
  60. ec = redis::error::resp3_null;
  61. break;
  62. default:
  63. BOOST_ASSERT_MSG(false, "Unexpected data type.");
  64. }
  65. throw system::system_error(ec, e.diagnostic);
  66. }
  67. } // boost::redis::adapter
  68. #endif // BOOST_REDIS_ADAPTER_RESULT_HPP