Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Errors.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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
21#pragma once
22
23#include <boost/json/object.hpp>
24#include <xrpl/protocol/ErrorCodes.h>
25
26#include <exception>
27#include <optional>
28#include <string>
29#include <string_view>
30#include <utility>
31#include <variant>
32
33namespace rpc {
34
36enum class ClioError {
37 // normal clio errors start with 5000
38 RpcMalformedCurrency = 5000,
39 RpcMalformedRequest = 5001,
40 RpcMalformedOwner = 5002,
41 RpcMalformedAddress = 5003,
42 RpcUnknownOption = 5005,
43 RpcFieldNotFoundTransaction = 5006,
44 RpcMalformedOracleDocumentId = 5007,
45 RpcMalformedAuthorizedCredentials = 5008,
46 RpcEntryNotFound = 5009,
47
48 // special system errors start with 6000
49 RpcInvalidApiVersion = 6000,
50 RpcCommandIsMissing = 6001,
51 RpcCommandNotString = 6002,
52 RpcCommandIsEmpty = 6003,
53 RpcParamsUnparsable = 6004,
54
55 // TODO: Since it is not only rpc errors here now, we should move it to util
56 // etl related errors start with 7000
57 // Higher value in this errors means better progress in the forwarding
58 EtlConnectionError = 7000,
59 EtlRequestError = 7001,
60 EtlRequestTimeout = 7002,
61 EtlInvalidResponse = 7003,
62};
63
66 ClioError const code;
67 std::string_view const error;
68 std::string_view const message;
69};
70
72using RippledError = ripple::error_code_i;
73
80using CombinedError = std::variant<RippledError, ClioError>;
81
83struct Status {
84 CombinedError code = RippledError::rpcSUCCESS;
85 std::string error;
86 std::string message;
87 std::optional<boost::json::object> extraInfo;
88
89 Status() = default;
90
96 /* implicit */ Status(CombinedError code) : code(code) {};
97
104 Status(CombinedError code, boost::json::object&& extraInfo) : code(code), extraInfo(std::move(extraInfo)) {};
105
114 explicit Status(std::string message) : code(ripple::rpcUNKNOWN), message(std::move(message))
115 {
116 }
117
124 Status(CombinedError code, std::string message) : code(code), message(std::move(message))
125 {
126 }
127
135 Status(CombinedError code, std::string error, std::string message)
136 : code(code), error(std::move(error)), message(std::move(message))
137 {
138 }
139
140 bool
141 operator==(Status const& other) const = default;
142
148 operator bool() const
149 {
150 if (auto err = std::get_if<RippledError>(&code))
151 return *err != RippledError::rpcSUCCESS;
152
153 return true;
154 }
155
162 bool
164 {
165 if (auto err = std::get_if<RippledError>(&code))
166 return *err == other;
167
168 return false;
169 }
170
177 bool
179 {
180 if (auto err = std::get_if<ClioError>(&code))
181 return *err == other;
182
183 return false;
184 }
185
193 friend std::ostream&
194 operator<<(std::ostream& stream, Status const& status);
195};
196
199 WarnUnknown = -1,
200 WarnRpcClio = 2001,
201 WarnRpcOutdated = 2002,
202 WarnRpcRateLimit = 2003,
203 WarnRpcDeprecated = 2004
204};
205
207struct WarningInfo {
208 constexpr WarningInfo() = default;
209
216 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
217 {
218 }
219
220 WarningCode code = WarnUnknown;
221 std::string_view const message = "unknown warning";
222};
223
225class InvalidParamsError : public std::exception {
226 std::string msg_;
227
228public:
234 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
235 {
236 }
237
243 char const*
244 what() const throw() override
245 {
246 return msg_.c_str();
247 }
248};
249
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 char const*
270 what() const throw() override
271 {
272 return account_.c_str();
273 }
274};
275
277static Status gOk;
278
285WarningInfo const&
287
294ClioErrorInfo const&
296
303boost::json::object
305
312boost::json::object
313makeError(Status const& status);
314
323boost::json::object
325 RippledError err,
326 std::optional<std::string_view> customError = std::nullopt,
327 std::optional<std::string_view> customMessage = std::nullopt
328);
329
338boost::json::object
340 ClioError err,
341 std::optional<std::string_view> customError = std::nullopt,
342 std::optional<std::string_view> customMessage = std::nullopt
343);
344
345} // namespace rpc
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:260
char const * what() const 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:234
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:244
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
ClioErrorInfo const & getErrorInfo(ClioError code)
Get the error info object from an clio-specific error code.
Definition Errors.cpp:112
static Status gOk
A globally available rpc::Status that represents a successful state.
Definition Errors.hpp:277
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:157
std::variant< RippledError, ClioError > CombinedError
Clio operates on a combination of Rippled and Custom Clio error codes.
Definition Errors.hpp:80
boost::json::object makeWarning(WarningCode code)
Generate JSON from a rpc::WarningCode.
Definition Errors.cpp:102
ripple::error_code_i RippledError
Clio uses compatible Rippled error codes for most RPC errors.
Definition Errors.hpp:72
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:198
WarningInfo const & getWarningInfo(WarningCode code)
Get the warning info object from a warning code.
Definition Errors.cpp:80
bool operator==(MaybeError const &lhs, MaybeError const &rhs)
Check if two MaybeError objects are equal.
Definition Types.hpp:65
ClioError
Custom clio RPC Errors.
Definition Errors.hpp:36
Holds info about a particular ClioError.
Definition Errors.hpp:65
A status returned from any RPC handler.
Definition Errors.hpp:83
Status(CombinedError code, boost::json::object &&extraInfo)
Construct a new Status object.
Definition Errors.hpp:104
bool operator==(RippledError other) const
Returns true if the rpc::Status contains the desired rpc::RippledError.
Definition Errors.hpp:163
Status(CombinedError code)
Construct a new Status object.
Definition Errors.hpp:96
bool operator==(ClioError other) const
Returns true if the Status contains the desired ClioError.
Definition Errors.hpp:178
Status(CombinedError code, std::string error, std::string message)
Construct a new Status object.
Definition Errors.hpp:135
Status(CombinedError code, std::string message)
Construct a new Status object.
Definition Errors.hpp:124
friend std::ostream & operator<<(std::ostream &stream, Status const &status)
Custom output stream for Status.
Definition Errors.cpp:45
Status(std::string message)
Construct a new Status object with a custom message.
Definition Errors.hpp:114
Holds information about a clio warning.
Definition Errors.hpp:207
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:216