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
17enum class ClioError {
18 // normal clio errors start with 5000
19 RpcMalformedCurrency = 5000,
20 RpcMalformedRequest = 5001,
21 RpcMalformedOwner = 5002,
22 RpcMalformedAddress = 5003,
23 RpcUnknownOption = 5005,
24 RpcFieldNotFoundTransaction = 5006,
25 RpcMalformedOracleDocumentId = 5007,
26 RpcMalformedAuthorizedCredentials = 5008,
27 // NOTE: RpcEntryNotFound is replaced with RippledError::rpcENTRY_NOT_FOUND
28 // RpcEntryNotFound = 5009,
29
30 // special system errors start with 6000
31 RpcInvalidApiVersion = 6000,
32 RpcCommandIsMissing = 6001,
33 RpcCommandNotString = 6002,
34 RpcCommandIsEmpty = 6003,
35 RpcParamsUnparsable = 6004,
36
37 // TODO: Since it is not only rpc errors here now, we should move it to util
38 // etl related errors start with 7000
39 // Higher value in this errors means better progress in the forwarding
40 EtlConnectionError = 7000,
41 EtlRequestError = 7001,
42 EtlRequestTimeout = 7002,
43 EtlInvalidResponse = 7003,
44};
45
48 ClioError const code;
49 std::string_view const error;
50 std::string_view const message;
51};
52
54using RippledError = ripple::error_code_i;
55
62using CombinedError = std::variant<RippledError, ClioError>;
63
65struct Status {
66 CombinedError code = RippledError::rpcSUCCESS;
67 std::string error;
68 std::string message;
69 std::optional<boost::json::object> extraInfo;
70
71 Status() = default;
72
78 /* implicit */ Status(CombinedError code) : code(code) {};
79
86 Status(CombinedError code, boost::json::object&& extraInfo)
87 : code(code), extraInfo(std::move(extraInfo)) {};
88
97 explicit Status(std::string message) : code(ripple::rpcUNKNOWN), message(std::move(message))
98 {
99 }
100
107 Status(CombinedError code, std::string message) : code(code), message(std::move(message))
108 {
109 }
110
118 Status(CombinedError code, std::string error, std::string message)
119 : code(code), error(std::move(error)), message(std::move(message))
120 {
121 }
122
123 bool
124 operator==(Status const& other) const = default;
125
131 operator bool() const
132 {
133 if (auto err = std::get_if<RippledError>(&code))
134 return *err != RippledError::rpcSUCCESS;
135
136 return true;
137 }
138
145 bool
147 {
148 if (auto err = std::get_if<RippledError>(&code))
149 return *err == other;
150
151 return false;
152 }
153
160 bool
162 {
163 if (auto err = std::get_if<ClioError>(&code))
164 return *err == other;
165
166 return false;
167 }
168
176 friend std::ostream&
177 operator<<(std::ostream& stream, Status const& status);
178};
179
182 WarnUnknown = -1,
183 WarnRpcClio = 2001,
184 WarnRpcOutdated = 2002,
185 WarnRpcRateLimit = 2003,
186 WarnRpcDeprecated = 2004
187};
188
190struct WarningInfo {
191 constexpr WarningInfo() = default;
192
199 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
200 {
201 }
202
203 WarningCode code = WarnUnknown;
204 std::string_view const message = "unknown warning";
205};
206
208class InvalidParamsError : public std::exception {
209 std::string msg_;
210
211public:
217 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
218 {
219 }
220
226 char const*
227 what() const throw() override
228 {
229 return msg_.c_str();
230 }
231};
232
234class AccountNotFoundError : public std::exception {
235 std::string account_;
236
237public:
243 explicit AccountNotFoundError(std::string acct) : account_(std::move(acct))
244 {
245 }
246
252 char const*
253 what() const throw() override
254 {
255 return account_.c_str();
256 }
257};
258
260static Status gOk;
261
268WarningInfo const&
270
277ClioErrorInfo const&
279
286boost::json::object
288
295boost::json::object
296makeError(Status const& status);
297
306boost::json::object
308 RippledError err,
309 std::optional<std::string_view> customError = std::nullopt,
310 std::optional<std::string_view> customMessage = std::nullopt
311);
312
321boost::json::object
323 ClioError err,
324 std::optional<std::string_view> customError = std::nullopt,
325 std::optional<std::string_view> customMessage = std::nullopt
326);
327
328} // namespace rpc
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:243
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:253
InvalidParamsError(std::string msg)
Construct a new Invalid Params Error object.
Definition Errors.hpp:217
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:227
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:18
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:260
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:62
boost::json::object makeWarning(WarningCode code)
Generate JSON from a rpc::WarningCode.
Definition Errors.cpp:84
ripple::error_code_i RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:54
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:181
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:17
Holds info about a particular ClioError.
Definition Errors.hpp:47
A status returned from any RPC handler.
Definition Errors.hpp:65
Status(CombinedError code, boost::json::object &&extraInfo)
Construct a new Status object.
Definition Errors.hpp:86
bool operator==(RippledError other) const
Returns true if the rpc::Status contains the desired rpc::RippledError.
Definition Errors.hpp:146
Status(CombinedError code)
Construct a new Status object.
Definition Errors.hpp:78
bool operator==(ClioError other) const
Returns true if the Status contains the desired ClioError.
Definition Errors.hpp:161
Status(CombinedError code, std::string error, std::string message)
Construct a new Status object.
Definition Errors.hpp:118
Status(CombinedError code, std::string message)
Construct a new Status object.
Definition Errors.hpp:107
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:97
Holds information about a clio warning.
Definition Errors.hpp:190
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:199