Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Error.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <cassandra.h>
23
24#include <cstdint>
25#include <ostream>
26#include <string>
27#include <utility>
28
29namespace data::cassandra {
30
35 std::string message_;
36 uint32_t code_{};
37
38public:
39 // default constructible required by Expected
40 CassandraError() = default;
41
48 CassandraError(std::string message, uint32_t code) : message_{std::move(message)}, code_{code}
49 {
50 }
51
53 template <typename T>
54 friend std::string
55 operator+(T const& lhs, CassandraError const& rhs)
56 requires std::is_convertible_v<T, std::string>
57 {
58 return lhs + rhs.message();
59 }
60
61 template <typename T>
62 friend bool
63 operator==(T const& lhs, CassandraError const& rhs)
64 requires std::is_convertible_v<T, std::string>
65 {
66 return lhs == rhs.message();
67 }
68
69 template <std::integral T>
70 friend bool
71 operator==(T const& lhs, CassandraError const& rhs)
72 {
73 return lhs == rhs.code();
74 }
75
76 friend std::ostream&
77 operator<<(std::ostream& os, CassandraError const& err)
78 {
79 os << err.message();
80 return os;
81 }
87 std::string
88 message() const
89 {
90 return message_;
91 }
92
96 uint32_t
97 code() const
98 {
99 return code_;
100 }
101
105 bool
106 isTimeout() const
107 {
108 return code_ == CASS_ERROR_LIB_NO_HOSTS_AVAILABLE or code_ == CASS_ERROR_LIB_REQUEST_TIMED_OUT or
109 code_ == CASS_ERROR_SERVER_UNAVAILABLE or code_ == CASS_ERROR_SERVER_OVERLOADED or
110 code_ == CASS_ERROR_SERVER_READ_TIMEOUT;
111 }
112
116 bool
118 {
119 return code_ == CASS_ERROR_SERVER_INVALID_QUERY;
120 }
121};
122
123} // namespace data::cassandra
A simple container for both error message and error code.
Definition Error.hpp:34
CassandraError(std::string message, uint32_t code)
Construct a new CassandraError object.
Definition Error.hpp:48
bool isTimeout() const
Definition Error.hpp:106
bool isInvalidQuery() const
Definition Error.hpp:117
uint32_t code() const
Definition Error.hpp:97
std::string message() const
Definition Error.hpp:88
This namespace implements a wrapper for the Cassandra C++ driver.
Definition Concepts.hpp:37
std::ostream & operator<<(std::ostream &stream, Severity sev)
Custom labels for Severity in log output.
Definition Logger.cpp:73