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};
186
189 WarnUnknown = -1,
190 WarnRpcClio = 2001,
191 WarnRpcOutdated = 2002,
192 WarnRpcRateLimit = 2003,
193 WarnRpcDeprecated = 2004
194};
195
198 constexpr WarningInfo() = default;
199
206 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
207 {
208 }
209
210 WarningCode code = WarnUnknown;
211 std::string_view const message = "unknown warning";
212};
213
215class InvalidParamsError : public std::exception {
216 std::string msg_;
217
218public:
224 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
225 {
226 }
227
233 char const*
234 what() const throw() override
235 {
236 return msg_.c_str();
237 }
238};
239
241class AccountNotFoundError : public std::exception {
242 std::string account_;
243
244public:
250 explicit AccountNotFoundError(std::string acct) : account_(std::move(acct))
251 {
252 }
253
259 char const*
260 what() const throw() override
261 {
262 return account_.c_str();
263 }
264};
265
267static Status gOk;
268
275WarningInfo const&
277
284ClioErrorInfo const&
286
293boost::json::object
295
302boost::json::object
303makeError(Status const& status);
304
313boost::json::object
315 RippledError err,
316 std::optional<std::string_view> customError = std::nullopt,
317 std::optional<std::string_view> customMessage = std::nullopt
318);
319
328boost::json::object
330 ClioError err,
331 std::optional<std::string_view> customError = std::nullopt,
332 std::optional<std::string_view> customMessage = std::nullopt
333);
334
335} // namespace rpc
Account not found error.
Definition Errors.hpp:241
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:250
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:260
Invalid parameters error.
Definition Errors.hpp:215
InvalidParamsError(std::string msg)
Construct a new Invalid Params Error object.
Definition Errors.hpp:224
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:234
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:75
static Status gOk
A globally available rpc::Status that represents a successful state.
Definition Errors.hpp:267
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:120
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:65
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:188
WarningInfo const & getWarningInfo(WarningCode code)
Get the warning info object from a warning code.
Definition Errors.cpp:43
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
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:197
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:206