Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:19

0001 
0002 /* Copyright (c) 2018-2023 Marcelo Zimbres Silva (mzimbres@gmail.com)
0003  *
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE.txt)
0006  */
0007 
0008 #ifndef BOOST_REDIS_ADAPTER_RESULT_HPP
0009 #define BOOST_REDIS_ADAPTER_RESULT_HPP
0010 
0011 #include <boost/redis/resp3/type.hpp>
0012 #include <boost/redis/error.hpp>
0013 #include <boost/system/result.hpp>
0014 #include <string>
0015 
0016 namespace boost::redis::adapter
0017 {
0018 
0019 /** @brief Stores any resp3 error
0020  *  @ingroup high-level-api
0021  */
0022 struct error {
0023    /// RESP3 error data type.
0024    resp3::type data_type = resp3::type::invalid;
0025 
0026    /// Diagnostic error message sent by Redis.
0027    std::string diagnostic;
0028 };
0029 
0030 /** @brief Compares two error objects for equality
0031  *  @relates error
0032  *
0033  *  @param a Left hand side error object.
0034  *  @param b Right hand side error object.
0035  */
0036 inline bool operator==(error const& a, error const& b)
0037 {
0038    return a.data_type == b.data_type && a.diagnostic == b.diagnostic;
0039 }
0040 
0041 /** @brief Compares two error objects for difference
0042  *  @relates error
0043  *
0044  *  @param a Left hand side error object.
0045  *  @param b Right hand side error object.
0046  */
0047 inline bool operator!=(error const& a, error const& b)
0048 {
0049    return !(a == b);
0050 }
0051 
0052 /** @brief Stores response to individual Redis commands
0053  *  @ingroup high-level-api
0054  */
0055 template <class Value>
0056 using result = system::result<Value, error>;
0057 
0058 BOOST_NORETURN inline void
0059 throw_exception_from_error(error const & e, boost::source_location const &)
0060 {
0061    system::error_code ec;
0062    switch (e.data_type) {
0063       case resp3::type::simple_error:
0064          ec = redis::error::resp3_simple_error;
0065          break;
0066       case resp3::type::blob_error:
0067          ec = redis::error::resp3_blob_error;
0068          break;
0069       case resp3::type::null:
0070          ec = redis::error::resp3_null;
0071          break;
0072       default:
0073          BOOST_ASSERT_MSG(false, "Unexpected data type.");
0074    }
0075 
0076    throw system::system_error(ec, e.diagnostic);
0077 }
0078 
0079 } // boost::redis::adapter
0080 
0081 #endif // BOOST_REDIS_ADAPTER_RESULT_HPP