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
181// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
183 WarnUnknown = -1,
184 WarnRpcClio = 2001,
185 WarnRpcOutdated = 2002,
186 WarnRpcRateLimit = 2003,
187 WarnRpcDeprecated = 2004
188};
189
191struct WarningInfo {
192 constexpr WarningInfo() = default;
193
200 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
201 {
202 }
203
204 WarningCode code = WarningCode::WarnUnknown;
205 std::string_view const message = "unknown warning";
206};
207
209class InvalidParamsError : public std::exception {
210 std::string msg_;
211
212public:
218 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
219 {
220 }
221
227 [[nodiscard]] char const*
228 what() const throw() override
229 {
230 return msg_.c_str();
231 }
232};
233
235class AccountNotFoundError : public std::exception {
236 std::string account_;
237
238public:
244 explicit AccountNotFoundError(std::string acct) : account_(std::move(acct))
245 {
246 }
247
253 [[nodiscard]] char const*
254 what() const throw() override
255 {
256 return account_.c_str();
257 }
258};
259
261static Status gOk;
262
269WarningInfo const&
271
278ClioErrorInfo const&
280
287boost::json::object
289
296boost::json::object
297makeError(Status const& status);
298
307boost::json::object
309 RippledError err,
310 std::optional<std::string_view> customError = std::nullopt,
311 std::optional<std::string_view> customMessage = std::nullopt
312);
313
322boost::json::object
324 ClioError err,
325 std::optional<std::string_view> customError = std::nullopt,
326 std::optional<std::string_view> customMessage = std::nullopt
327);
328
329} // namespace rpc
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:244
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:254
InvalidParamsError(std::string msg)
Construct a new Invalid Params Error object.
Definition Errors.hpp:218
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:228
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:261
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:182
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:191
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:200