Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Errors.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <boost/json/object.hpp>
5#include <xrpl/protocol/ErrorCodes.h>
6
7#include <exception>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <variant>
13
14namespace rpc {
15
19enum class ClioError {
20 // normal clio errors start with 5000
21 RpcMalformedCurrency = 5000,
22 RpcMalformedRequest = 5001,
23 RpcMalformedOwner = 5002,
24 RpcMalformedAddress = 5003,
25 RpcUnknownOption = 5005,
26 RpcFieldNotFoundTransaction = 5006,
27 RpcMalformedOracleDocumentId = 5007,
28 RpcMalformedAuthorizedCredentials = 5008,
29 // NOTE: RpcEntryNotFound is replaced with RippledError::RpcEntryNotFound
30 // RpcEntryNotFound = 5009,
31
32 // special system errors start with 6000
33 RpcInvalidApiVersion = 6000,
34 RpcCommandIsMissing = 6001,
35 RpcCommandNotString = 6002,
36 RpcCommandIsEmpty = 6003,
37 RpcParamsUnparsable = 6004,
38
39 // TODO: Since it is not only rpc errors here now, we should move it to util
40 // etl related errors start with 7000
41 // Higher value in this errors means better progress in the forwarding
42 EtlConnectionError = 7000,
43 EtlRequestError = 7001,
44 EtlRequestTimeout = 7002,
45 EtlInvalidResponse = 7003,
46};
47
52 ClioError const code;
53 std::string_view const error;
54 std::string_view const message;
55};
56
60using RippledError = xrpl::ErrorCodeI;
61
68using CombinedError = std::variant<RippledError, ClioError>;
69
73struct Status {
74 CombinedError code = RippledError::RpcSuccess;
75 std::string error;
76 std::string message;
77 std::optional<boost::json::object> extraInfo;
78
79 Status() = default;
80
86 /* implicit */ Status(CombinedError code) : code(code) {};
87
94 Status(CombinedError code, boost::json::object&& extraInfo)
95 : code(code), extraInfo(std::move(extraInfo)) {};
96
105 explicit Status(std::string message) : code(xrpl::RpcUnknown), message(std::move(message))
106 {
107 }
108
115 Status(CombinedError code, std::string message) : code(code), message(std::move(message))
116 {
117 }
118
126 Status(CombinedError code, std::string error, std::string message)
127 : code(code), error(std::move(error)), message(std::move(message))
128 {
129 }
130
131 bool
132 operator==(Status const& other) const = default;
133
139 operator bool() const
140 {
141 if (auto err = std::get_if<RippledError>(&code))
142 return *err != RippledError::RpcSuccess;
143
144 return true;
145 }
146
153 bool
155 {
156 if (auto err = std::get_if<RippledError>(&code))
157 return *err == other;
158
159 return false;
160 }
161
168 bool
170 {
171 if (auto err = std::get_if<ClioError>(&code))
172 return *err == other;
173
174 return false;
175 }
176
184 friend std::ostream&
185 operator<<(std::ostream& stream, Status const& status);
186};
187
191// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
193 WarnUnknown = -1,
194 WarnRpcClio = 2001,
195 WarnRpcOutdated = 2002,
196 WarnRpcRateLimit = 2003,
197 WarnRpcDeprecated = 2004
198};
199
203struct WarningInfo {
204 constexpr WarningInfo() = default;
205
212 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
213 {
214 }
215
216 WarningCode code = WarningCode::WarnUnknown;
217 std::string_view const message = "unknown warning";
218};
219
223class InvalidParamsError : public std::exception {
224 std::string msg_;
225
226public:
232 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
233 {
234 }
235
241 [[nodiscard]] char const*
242 what() const noexcept override
243 {
244 return msg_.c_str();
245 }
246};
247
251class AccountNotFoundError : public std::exception {
252 std::string account_;
253
254public:
260 explicit AccountNotFoundError(std::string acct) : account_(std::move(acct))
261 {
262 }
263
269 [[nodiscard]] char const*
270 what() const noexcept override
271 {
272 return account_.c_str();
273 }
274};
275
279static Status gOk;
280
287WarningInfo const&
289
296ClioErrorInfo const&
298
305boost::json::object
307
314boost::json::object
315makeError(Status const& status);
316
325boost::json::object
327 RippledError err,
328 std::optional<std::string_view> customError = std::nullopt,
329 std::optional<std::string_view> customMessage = std::nullopt
330);
331
340boost::json::object
342 ClioError err,
343 std::optional<std::string_view> customError = std::nullopt,
344 std::optional<std::string_view> customMessage = std::nullopt
345);
346
347} // namespace rpc
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:260
char const * what() const noexcept override
Get the error message as a C string.
Definition Errors.hpp:270
InvalidParamsError(std::string msg)
Construct a new Invalid Params Error object.
Definition Errors.hpp:232
char const * what() const noexcept override
Get the error message as a C string.
Definition Errors.hpp:242
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
ClioErrorInfo const & getErrorInfo(ClioError code)
Get the error info object from an clio-specific error code.
Definition Errors.cpp:94
static Status gOk
A globally available rpc::Status that represents a successful state.
Definition Errors.hpp:279
boost::json::object makeError(RippledError err, std::optional< std::string_view > customError, std::optional< std::string_view > customMessage)
Generate JSON from a rpc::RippledError.
Definition Errors.cpp:160
std::variant< RippledError, ClioError > CombinedError
Clio operates on a combination of Rippled and Custom Clio error codes.
Definition Errors.hpp:68
xrpl::ErrorCodeI RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:60
boost::json::object makeWarning(WarningCode code)
Generate JSON from a rpc::WarningCode.
Definition Errors.cpp:84
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:192
WarningInfo const & getWarningInfo(WarningCode code)
Get the warning info object from a warning code.
Definition Errors.cpp:61
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:46
ClioError
Custom clio RPC Errors.
Definition Errors.hpp:19
Holds info about a particular ClioError.
Definition Errors.hpp:51
A status returned from any RPC handler.
Definition Errors.hpp:73
Status(CombinedError code, boost::json::object &&extraInfo)
Construct a new Status object.
Definition Errors.hpp:94
bool operator==(RippledError other) const
Returns true if the rpc::Status contains the desired rpc::RippledError.
Definition Errors.hpp:154
Status(CombinedError code)
Construct a new Status object.
Definition Errors.hpp:86
bool operator==(ClioError other) const
Returns true if the Status contains the desired ClioError.
Definition Errors.hpp:169
Status(CombinedError code, std::string error, std::string message)
Construct a new Status object.
Definition Errors.hpp:126
Status(CombinedError code, std::string message)
Construct a new Status object.
Definition Errors.hpp:115
friend std::ostream & operator<<(std::ostream &stream, Status const &status)
Custom output stream for Status.
Definition Errors.cpp:26
Status(std::string message)
Construct a new Status object with a custom message.
Definition Errors.hpp:105
Holds information about a clio warning.
Definition Errors.hpp:203
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:212