rippled
Loading...
Searching...
No Matches
ErrorCodes.cpp
1#include <xrpl/beast/utility/instrumentation.h>
2#include <xrpl/json/json_value.h>
3#include <xrpl/protocol/ErrorCodes.h>
4#include <xrpl/protocol/jss.h>
5
6#include <array>
7#include <stdexcept>
8#include <string>
9
10namespace ripple {
11namespace RPC {
12
13namespace detail {
14
15// Unordered array of ErrorInfos, so we don't have to maintain the list
16// ordering by hand.
17//
18// This array will be omitted from the object file; only the sorted version
19// will remain in the object file. But the string literals will remain.
20//
21// There's a certain amount of tension in determining the correct HTTP
22// status to associate with a given RPC error. Initially all RPC errors
23// returned 200 (OK). And that's the default behavior if no HTTP status code
24// is specified below.
25//
26// The codes currently selected target the load balancer fail-over use case.
27// If a query fails on one node but is likely to have a positive outcome
28// on a different node, then the failure should return a 4xx/5xx range
29// status code.
30
31// clang-format off
32constexpr static ErrorInfo unorderedErrorInfos[]{
33 {rpcACT_MALFORMED, "actMalformed", "Account malformed."},
34 {rpcACT_NOT_FOUND, "actNotFound", "Account not found."},
35 {rpcALREADY_MULTISIG, "alreadyMultisig", "Already multisigned."},
36 {rpcALREADY_SINGLE_SIG, "alreadySingleSig", "Already single-signed."},
37 {rpcAMENDMENT_BLOCKED, "amendmentBlocked", "Amendment blocked, need upgrade.", 503},
38 {rpcEXPIRED_VALIDATOR_LIST, "unlBlocked", "Validator list expired.", 503},
39 {rpcATX_DEPRECATED, "deprecated", "Use the new API or specify a ledger range.", 400},
40 {rpcBAD_KEY_TYPE, "badKeyType", "Bad key type.", 400},
41 {rpcBAD_FEATURE, "badFeature", "Feature unknown or invalid.", 500},
42 {rpcBAD_ISSUER, "badIssuer", "Issuer account malformed.", 400},
43 {rpcBAD_MARKET, "badMarket", "No such market.", 404},
44 {rpcBAD_SECRET, "badSecret", "Secret does not match account.", 403},
45 {rpcBAD_SEED, "badSeed", "Disallowed seed.", 403},
46 {rpcBAD_SYNTAX, "badSyntax", "Syntax error.", 400},
47 {rpcCHANNEL_MALFORMED, "channelMalformed", "Payment channel is malformed.", 400},
48 {rpcCHANNEL_AMT_MALFORMED, "channelAmtMalformed", "Payment channel amount is malformed.", 400},
49 {rpcCOMMAND_MISSING, "commandMissing", "Missing command entry.", 400},
50 {rpcDB_DESERIALIZATION, "dbDeserialization", "Database deserialization error.", 502},
51 {rpcDST_ACT_MALFORMED, "dstActMalformed", "Destination account is malformed.", 400},
52 {rpcDST_ACT_MISSING, "dstActMissing", "Destination account not provided.", 400},
53 {rpcDST_ACT_NOT_FOUND, "dstActNotFound", "Destination account not found.", 404},
54 {rpcDST_AMT_MALFORMED, "dstAmtMalformed", "Destination amount/currency/issuer is malformed.", 400},
55 {rpcDST_AMT_MISSING, "dstAmtMissing", "Destination amount/currency/issuer is missing.", 400},
56 {rpcDST_ISR_MALFORMED, "dstIsrMalformed", "Destination issuer is malformed.", 400},
57 {rpcEXCESSIVE_LGR_RANGE, "excessiveLgrRange", "Ledger range exceeds 1000.", 400},
58 {rpcFORBIDDEN, "forbidden", "Bad credentials.", 403},
59 {rpcHIGH_FEE, "highFee", "Current transaction fee exceeds your limit.", 402},
60 {rpcINTERNAL, "internal", "Internal error.", 500},
61 {rpcINVALID_LGR_RANGE, "invalidLgrRange", "Ledger range is invalid.", 400},
62 {rpcINVALID_PARAMS, "invalidParams", "Invalid parameters.", 400},
63 {rpcINVALID_HOTWALLET, "invalidHotWallet", "Invalid hotwallet.", 400},
64 {rpcISSUE_MALFORMED, "issueMalformed", "Issue is malformed.", 400},
65 {rpcJSON_RPC, "json_rpc", "JSON-RPC transport error.", 500},
66 {rpcLGR_IDXS_INVALID, "lgrIdxsInvalid", "Ledger indexes invalid.", 400},
67 {rpcLGR_IDX_MALFORMED, "lgrIdxMalformed", "Ledger index malformed.", 400},
68 {rpcLGR_NOT_FOUND, "lgrNotFound", "Ledger not found.", 404},
69 {rpcLGR_NOT_VALIDATED, "lgrNotValidated", "Ledger not validated.", 202},
70 {rpcMASTER_DISABLED, "masterDisabled", "Master key is disabled.", 403},
71 {rpcNOT_ENABLED, "notEnabled", "Not enabled in configuration.", 501},
72 {rpcNOT_IMPL, "notImpl", "Not implemented.", 501},
73 {rpcNOT_READY, "notReady", "Not ready to handle this request.", 503},
74 {rpcNOT_SUPPORTED, "notSupported", "Operation not supported.", 501},
75 {rpcNO_CLOSED, "noClosed", "Closed ledger is unavailable.", 503},
76 {rpcNO_CURRENT, "noCurrent", "Current ledger is unavailable.", 503},
77 {rpcNOT_SYNCED, "notSynced", "Not synced to the network.", 503},
78 {rpcNO_EVENTS, "noEvents", "Current transport does not support events.", 405},
79 {rpcNO_NETWORK, "noNetwork", "Not synced to the network.", 503},
80 {rpcWRONG_NETWORK, "wrongNetwork", "Wrong network.", 503},
81 {rpcNO_PERMISSION, "noPermission", "You don't have permission for this command.", 401},
82 {rpcNO_PF_REQUEST, "noPathRequest", "No pathfinding request in progress.", 404},
83 {rpcOBJECT_NOT_FOUND, "objectNotFound", "The requested object was not found.", 404},
84 {rpcPUBLIC_MALFORMED, "publicMalformed", "Public key is malformed.", 400},
85 {rpcSENDMAX_MALFORMED, "sendMaxMalformed", "SendMax amount malformed.", 400},
86 {rpcSIGNING_MALFORMED, "signingMalformed", "Signing of transaction is malformed.", 400},
87 {rpcSLOW_DOWN, "slowDown", "You are placing too much load on the server.", 429},
88 {rpcSRC_ACT_MALFORMED, "srcActMalformed", "Source account is malformed.", 400},
89 {rpcSRC_ACT_MISSING, "srcActMissing", "Source account not provided.", 400},
90 {rpcSRC_ACT_NOT_FOUND, "srcActNotFound", "Source account not found.", 404},
91 {rpcDELEGATE_ACT_NOT_FOUND, "delegateActNotFound", "Delegate account not found.", 404},
92 {rpcSRC_CUR_MALFORMED, "srcCurMalformed", "Source currency is malformed.", 400},
93 {rpcSRC_ISR_MALFORMED, "srcIsrMalformed", "Source issuer is malformed.", 400},
94 {rpcSTREAM_MALFORMED, "malformedStream", "Stream malformed.", 400},
95 {rpcTOO_BUSY, "tooBusy", "The server is too busy to help you now.", 503},
96 {rpcTXN_NOT_FOUND, "txnNotFound", "Transaction not found.", 404},
97 {rpcUNKNOWN_COMMAND, "unknownCmd", "Unknown method.", 405},
98 {rpcORACLE_MALFORMED, "oracleMalformed", "Oracle request is malformed.", 400},
99 {rpcBAD_CREDENTIALS, "badCredentials", "Credentials do not exist, are not accepted, or have expired.", 400},
100 {rpcTX_SIGNED, "transactionSigned", "Transaction should not be signed.", 400},
101 {rpcDOMAIN_MALFORMED, "domainMalformed", "Domain is malformed.", 400},
102 {rpcENTRY_NOT_FOUND, "entryNotFound", "Entry not found.", 400},
103 {rpcUNEXPECTED_LEDGER_TYPE, "unexpectedLedgerType", "Unexpected ledger type.", 400},
104};
105// clang-format on
106
107// Sort and validate unorderedErrorInfos at compile time. Should be
108// converted to consteval when get to C++20.
109template <int M, int N>
110constexpr auto
112{
114
115 for (ErrorInfo const& info : unordered)
116 {
117 if (info.code <= rpcSUCCESS || info.code > rpcLAST)
118 throw(std::out_of_range("Invalid error_code_i"));
119
120 // The first valid code follows rpcSUCCESS immediately.
121 static_assert(rpcSUCCESS == 0, "Unexpected error_code_i layout.");
122 int const index{info.code - 1};
123
124 if (ret[index].code != rpcUNKNOWN)
125 throw(std::invalid_argument("Duplicate error_code_i in list"));
126
127 ret[index] = info;
128 }
129
130 // Verify that all entries are filled in starting with 1 and proceeding
131 // to rpcLAST.
132 //
133 // It's okay for there to be missing entries; they will contain the code
134 // rpcUNKNOWN. But other than that all entries should match their index.
135 int codeCount{0};
136 int expect{rpcBAD_SYNTAX - 1};
137 for (ErrorInfo const& info : ret)
138 {
139 ++expect;
140 if (info.code == rpcUNKNOWN)
141 continue;
142
143 if (info.code != expect)
144 throw(std::invalid_argument("Empty error_code_i in list"));
145 ++codeCount;
146 }
147 if (expect != rpcLAST)
148 throw(std::invalid_argument("Insufficient list entries"));
149 if (codeCount != N)
150 throw(std::invalid_argument("Bad handling of unorderedErrorInfos"));
151
152 return ret;
153}
154
155constexpr auto sortedErrorInfos{sortErrorInfos<rpcLAST>(unorderedErrorInfos)};
156
158
159} // namespace detail
160
161//------------------------------------------------------------------------------
162
163ErrorInfo const&
165{
166 if (code <= rpcSUCCESS || code > rpcLAST)
168 return detail::sortedErrorInfos[code - 1];
169}
170
173{
174 Json::Value json;
175 inject_error(code, json);
176 return json;
177}
178
181{
182 Json::Value json;
183 inject_error(code, message, json);
184 return json;
185}
186
187bool
189{
190 if (json.isObject() && json.isMember(jss::error))
191 return true;
192 return false;
193}
194
195int
200
201} // namespace RPC
202
205{
206 XRPL_ASSERT(
208 "ripple::RPC::rpcErrorString : input contains an error");
209 return jv[jss::error].asString() + jv[jss::error_message].asString();
210}
211
212} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
bool isObject() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
constexpr auto sortedErrorInfos
constexpr ErrorInfo unknownError
constexpr auto sortErrorInfos(ErrorInfo const (&unordered)[N]) -> std::array< ErrorInfo, M >
static constexpr ErrorInfo unorderedErrorInfos[]
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:214
int error_code_http_status(error_code_i code)
Returns http status that corresponds to the error code.
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
@ rpcNO_EVENTS
Definition ErrorCodes.h:35
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:51
@ rpcDST_ACT_MISSING
Definition ErrorCodes.h:85
@ rpcBAD_ISSUER
Definition ErrorCodes.h:77
@ rpcNOT_SUPPORTED
Definition ErrorCodes.h:113
@ rpcALREADY_SINGLE_SIG
Definition ErrorCodes.h:73
@ rpcNO_NETWORK
Definition ErrorCodes.h:47
@ rpcISSUE_MALFORMED
Definition ErrorCodes.h:127
@ rpcEXCESSIVE_LGR_RANGE
Definition ErrorCodes.h:116
@ rpcATX_DEPRECATED
Definition ErrorCodes.h:108
@ rpcORACLE_MALFORMED
Definition ErrorCodes.h:130
@ rpcAMENDMENT_BLOCKED
Definition ErrorCodes.h:42
@ rpcINVALID_LGR_RANGE
Definition ErrorCodes.h:117
@ rpcUNKNOWN_COMMAND
Definition ErrorCodes.h:66
@ rpcTXN_NOT_FOUND
Definition ErrorCodes.h:61
@ rpcLGR_NOT_VALIDATED
Definition ErrorCodes.h:54
@ rpcTOO_BUSY
Definition ErrorCodes.h:37
@ rpcSRC_ACT_NOT_FOUND
Definition ErrorCodes.h:103
@ rpcDELEGATE_ACT_NOT_FOUND
Definition ErrorCodes.h:104
@ rpcENTRY_NOT_FOUND
Definition ErrorCodes.h:142
@ rpcTX_SIGNED
Definition ErrorCodes.h:136
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
@ rpcSLOW_DOWN
Definition ErrorCodes.h:38
@ rpcMASTER_DISABLED
Definition ErrorCodes.h:55
@ rpcBAD_FEATURE
Definition ErrorCodes.h:76
@ rpcBAD_KEY_TYPE
Definition ErrorCodes.h:114
@ rpcCHANNEL_AMT_MALFORMED
Definition ErrorCodes.h:82
@ rpcALREADY_MULTISIG
Definition ErrorCodes.h:72
@ rpcBAD_MARKET
Definition ErrorCodes.h:78
@ rpcOBJECT_NOT_FOUND
Definition ErrorCodes.h:124
@ rpcNOT_READY
Definition ErrorCodes.h:41
@ rpcSUCCESS
Definition ErrorCodes.h:25
@ rpcSENDMAX_MALFORMED
Definition ErrorCodes.h:100
@ rpcNO_CURRENT
Definition ErrorCodes.h:46
@ rpcPUBLIC_MALFORMED
Definition ErrorCodes.h:98
@ rpcEXPIRED_VALIDATOR_LIST
Definition ErrorCodes.h:118
@ rpcDOMAIN_MALFORMED
Definition ErrorCodes.h:139
@ rpcDST_ISR_MALFORMED
Definition ErrorCodes.h:89
@ rpcBAD_CREDENTIALS
Definition ErrorCodes.h:133
@ rpcINVALID_PARAMS
Definition ErrorCodes.h:65
@ rpcINTERNAL
Definition ErrorCodes.h:111
@ rpcBAD_SECRET
Definition ErrorCodes.h:79
@ rpcBAD_SYNTAX
Definition ErrorCodes.h:27
@ rpcSTREAM_MALFORMED
Definition ErrorCodes.h:107
@ rpcLGR_NOT_FOUND
Definition ErrorCodes.h:53
@ rpcLGR_IDXS_INVALID
Definition ErrorCodes.h:93
@ rpcSRC_ACT_MALFORMED
Definition ErrorCodes.h:101
@ rpcNOT_IMPL
Definition ErrorCodes.h:112
@ rpcSRC_ISR_MALFORMED
Definition ErrorCodes.h:106
@ rpcSIGNING_MALFORMED
Definition ErrorCodes.h:99
@ rpcUNKNOWN
Definition ErrorCodes.h:23
@ rpcCHANNEL_MALFORMED
Definition ErrorCodes.h:81
@ rpcNO_PF_REQUEST
Definition ErrorCodes.h:67
@ rpcDST_AMT_MALFORMED
Definition ErrorCodes.h:87
@ rpcLGR_IDX_MALFORMED
Definition ErrorCodes.h:94
@ rpcCOMMAND_MISSING
Definition ErrorCodes.h:83
@ rpcFORBIDDEN
Definition ErrorCodes.h:29
@ rpcNO_CLOSED
Definition ErrorCodes.h:45
@ rpcJSON_RPC
Definition ErrorCodes.h:28
@ rpcNOT_ENABLED
Definition ErrorCodes.h:40
@ rpcDST_ACT_MALFORMED
Definition ErrorCodes.h:84
@ rpcWRONG_NETWORK
Definition ErrorCodes.h:31
@ rpcUNEXPECTED_LEDGER_TYPE
Definition ErrorCodes.h:143
@ rpcSRC_CUR_MALFORMED
Definition ErrorCodes.h:105
@ rpcINVALID_HOTWALLET
Definition ErrorCodes.h:62
@ rpcBAD_SEED
Definition ErrorCodes.h:80
@ rpcDST_AMT_MISSING
Definition ErrorCodes.h:88
@ rpcHIGH_FEE
Definition ErrorCodes.h:39
@ rpcDB_DESERIALIZATION
Definition ErrorCodes.h:115
@ rpcDST_ACT_NOT_FOUND
Definition ErrorCodes.h:86
@ rpcNOT_SYNCED
Definition ErrorCodes.h:48
@ rpcNO_PERMISSION
Definition ErrorCodes.h:34
@ rpcSRC_ACT_MISSING
Definition ErrorCodes.h:102
Maps an rpc error code to its token, default message, and HTTP status.
Definition ErrorCodes.h:170