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
47 // special system errors start with 6000
48 RpcInvalidApiVersion = 6000,
49 RpcCommandIsMissing = 6001,
50 RpcCommandNotString = 6002,
51 RpcCommandIsEmpty = 6003,
52 RpcParamsUnparseable = 6004,
53
54 // TODO: Since it is not only rpc errors here now, we should move it to util
55 // etl related errors start with 7000
56 // Higher value in this errors means better progress in the forwarding
57 EtlConnectionError = 7000,
58 EtlRequestError = 7001,
59 EtlRequestTimeout = 7002,
60 EtlInvalidResponse = 7003,
61};
62
65 ClioError const code;
66 std::string_view const error;
67 std::string_view const message;
68};
69
71using RippledError = ripple::error_code_i;
72
79using CombinedError = std::variant<RippledError, ClioError>;
80
82struct Status {
83 CombinedError code = RippledError::rpcSUCCESS;
84 std::string error;
85 std::string message;
86 std::optional<boost::json::object> extraInfo;
87
88 Status() = default;
89
95 /* implicit */ Status(CombinedError code) : code(code) {};
96
103 Status(CombinedError code, boost::json::object&& extraInfo) : code(code), extraInfo(std::move(extraInfo)) {};
104
113 explicit Status(std::string message) : code(ripple::rpcUNKNOWN), message(std::move(message))
114 {
115 }
116
123 Status(CombinedError code, std::string message) : code(code), message(std::move(message))
124 {
125 }
126
134 Status(CombinedError code, std::string error, std::string message)
135 : code(code), error(std::move(error)), message(std::move(message))
136 {
137 }
138
139 bool
140 operator==(Status const& other) const = default;
141
147 operator bool() const
148 {
149 if (auto err = std::get_if<RippledError>(&code))
150 return *err != RippledError::rpcSUCCESS;
151
152 return true;
153 }
154
161 bool
163 {
164 if (auto err = std::get_if<RippledError>(&code))
165 return *err == other;
166
167 return false;
168 }
169
176 bool
178 {
179 if (auto err = std::get_if<ClioError>(&code))
180 return *err == other;
181
182 return false;
183 }
184};
185
188 WarnUnknown = -1,
189 WarnRpcClio = 2001,
190 WarnRpcOutdated = 2002,
191 WarnRpcRateLimit = 2003,
192 WarnRpcDeprecated = 2004
193};
194
197 constexpr WarningInfo() = default;
198
205 constexpr WarningInfo(WarningCode code, char const* message) : code(code), message(message)
206 {
207 }
208
209 WarningCode code = WarnUnknown;
210 std::string_view const message = "unknown warning";
211};
212
214class InvalidParamsError : public std::exception {
215 std::string msg_;
216
217public:
223 explicit InvalidParamsError(std::string msg) : msg_(std::move(msg))
224 {
225 }
226
232 char const*
233 what() const throw() override
234 {
235 return msg_.c_str();
236 }
237};
238
240class AccountNotFoundError : public std::exception {
241 std::string account_;
242
243public:
249 explicit AccountNotFoundError(std::string acct) : account_(std::move(acct))
250 {
251 }
252
258 char const*
259 what() const throw() override
260 {
261 return account_.c_str();
262 }
263};
264
266static Status gOk;
267
274WarningInfo const&
276
283ClioErrorInfo const&
285
292boost::json::object
294
301boost::json::object
302makeError(Status const& status);
303
312boost::json::object
314 RippledError err,
315 std::optional<std::string_view> customError = std::nullopt,
316 std::optional<std::string_view> customMessage = std::nullopt
317);
318
327boost::json::object
329 ClioError err,
330 std::optional<std::string_view> customError = std::nullopt,
331 std::optional<std::string_view> customMessage = std::nullopt
332);
333
334} // namespace rpc
Account not found error.
Definition Errors.hpp:240
AccountNotFoundError(std::string acct)
Construct a new Account Not Found Error object.
Definition Errors.hpp:249
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:259
Invalid parameters error.
Definition Errors.hpp:214
InvalidParamsError(std::string msg)
Construct a new Invalid Params Error object.
Definition Errors.hpp:223
char const * what() const override
Get the error message as a C string.
Definition Errors.hpp:233
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:36
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:266
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:79
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:71
WarningCode
Warning codes that can be returned by clio.
Definition Errors.hpp:187
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:64
A status returned from any RPC handler.
Definition Errors.hpp:82
Status(CombinedError code, boost::json::object &&extraInfo)
Construct a new Status object.
Definition Errors.hpp:103
bool operator==(RippledError other) const
Returns true if the rpc::Status contains the desired rpc::RippledError.
Definition Errors.hpp:162
Status(CombinedError code)
Construct a new Status object.
Definition Errors.hpp:95
bool operator==(ClioError other) const
Returns true if the Status contains the desired ClioError.
Definition Errors.hpp:177
Status(CombinedError code, std::string error, std::string message)
Construct a new Status object.
Definition Errors.hpp:134
Status(CombinedError code, std::string message)
Construct a new Status object.
Definition Errors.hpp:123
Status(std::string message)
Construct a new Status object with a custom message.
Definition Errors.hpp:113
Holds information about a clio warning.
Definition Errors.hpp:196
constexpr WarningInfo(WarningCode code, char const *message)
Construct a new Warning Info object.
Definition Errors.hpp:205