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