rippled
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 <ripple/protocol/ErrorCodes.h>
21 #include <cassert>
22 #include <stdexcept>
23 
24 namespace ripple {
25 namespace RPC {
26 
27 namespace detail {
28 
29 // clang-format off
30 // Unordered array of ErrorInfos, so we don't have to maintain the list
31 // ordering by hand.
32 //
33 // This array will be omitted from the object file; only the sorted version
34 // will remain in the object file. But the string literals will remain.
35 constexpr static ErrorInfo unorderedErrorInfos[]{
36  {rpcACT_MALFORMED, "actMalformed", "Account malformed."},
37  {rpcACT_NOT_FOUND, "actNotFound", "Account not found."},
38  {rpcALREADY_MULTISIG, "alreadyMultisig", "Already multisigned."},
39  {rpcALREADY_SINGLE_SIG, "alreadySingleSig", "Already single-signed."},
40  {rpcAMENDMENT_BLOCKED, "amendmentBlocked", "Amendment blocked, need upgrade."},
41  {rpcEXPIRED_VALIDATOR_LIST, "unlBlocked", "Validator list expired."},
42  {rpcATX_DEPRECATED, "deprecated", "Use the new API or specify a ledger range."},
43  {rpcBAD_KEY_TYPE, "badKeyType", "Bad key type."},
44  {rpcBAD_FEATURE, "badFeature", "Feature unknown or invalid."},
45  {rpcBAD_ISSUER, "badIssuer", "Issuer account malformed."},
46  {rpcBAD_MARKET, "badMarket", "No such market."},
47  {rpcBAD_SECRET, "badSecret", "Secret does not match account."},
48  {rpcBAD_SEED, "badSeed", "Disallowed seed."},
49  {rpcBAD_SYNTAX, "badSyntax", "Syntax error."},
50  {rpcCHANNEL_MALFORMED, "channelMalformed", "Payment channel is malformed."},
51  {rpcCHANNEL_AMT_MALFORMED, "channelAmtMalformed", "Payment channel amount is malformed."},
52  {rpcCOMMAND_MISSING, "commandMissing", "Missing command entry."},
53  {rpcDB_DESERIALIZATION, "dbDeserialization", "Database deserialization error."},
54  {rpcDST_ACT_MALFORMED, "dstActMalformed", "Destination account is malformed."},
55  {rpcDST_ACT_MISSING, "dstActMissing", "Destination account not provided."},
56  {rpcDST_ACT_NOT_FOUND, "dstActNotFound", "Destination account not found."},
57  {rpcDST_AMT_MALFORMED, "dstAmtMalformed", "Destination amount/currency/issuer is malformed."},
58  {rpcDST_AMT_MISSING, "dstAmtMissing", "Destination amount/currency/issuer is missing."},
59  {rpcDST_ISR_MALFORMED, "dstIsrMalformed", "Destination issuer is malformed."},
60  {rpcEXCESSIVE_LGR_RANGE, "excessiveLgrRange", "Ledger range exceeds 1000."},
61  {rpcFORBIDDEN, "forbidden", "Bad credentials."},
62  {rpcFAILED_TO_FORWARD, "failedToForward", "Failed to forward request to p2p node"},
63  {rpcHIGH_FEE, "highFee", "Current transaction fee exceeds your limit."},
64  {rpcINTERNAL, "internal", "Internal error."},
65  {rpcINVALID_LGR_RANGE, "invalidLgrRange", "Ledger range is invalid."},
66  {rpcINVALID_PARAMS, "invalidParams", "Invalid parameters."},
67  {rpcJSON_RPC, "json_rpc", "JSON-RPC transport error."},
68  {rpcLGR_IDXS_INVALID, "lgrIdxsInvalid", "Ledger indexes invalid."},
69  {rpcLGR_IDX_MALFORMED, "lgrIdxMalformed", "Ledger index malformed."},
70  {rpcLGR_NOT_FOUND, "lgrNotFound", "Ledger not found."},
71  {rpcLGR_NOT_VALIDATED, "lgrNotValidated", "Ledger not validated."},
72  {rpcMASTER_DISABLED, "masterDisabled", "Master key is disabled."},
73  {rpcNOT_ENABLED, "notEnabled", "Not enabled in configuration."},
74  {rpcNOT_IMPL, "notImpl", "Not implemented."},
75  {rpcNOT_READY, "notReady", "Not ready to handle this request."},
76  {rpcNOT_SUPPORTED, "notSupported", "Operation not supported."},
77  {rpcNO_CLOSED, "noClosed", "Closed ledger is unavailable."},
78  {rpcNO_CURRENT, "noCurrent", "Current ledger is unavailable."},
79  {rpcNOT_SYNCED, "notSynced", "Not synced to the network."},
80  {rpcNO_EVENTS, "noEvents", "Current transport does not support events."},
81  {rpcNO_NETWORK, "noNetwork", "Not synced to the network."},
82  {rpcNO_PERMISSION, "noPermission", "You don't have permission for this command."},
83  {rpcNO_PF_REQUEST, "noPathRequest", "No pathfinding request in progress."},
84  {rpcPUBLIC_MALFORMED, "publicMalformed", "Public key is malformed."},
85  {rpcREPORTING_UNSUPPORTED, "reportingUnsupported", "Requested operation not supported by reporting mode server"},
86  {rpcSIGNING_MALFORMED, "signingMalformed", "Signing of transaction is malformed."},
87  {rpcSLOW_DOWN, "slowDown", "You are placing too much load on the server."},
88  {rpcSRC_ACT_MALFORMED, "srcActMalformed", "Source account is malformed."},
89  {rpcSRC_ACT_MISSING, "srcActMissing", "Source account not provided."},
90  {rpcSRC_ACT_NOT_FOUND, "srcActNotFound", "Source account not found."},
91  {rpcSRC_CUR_MALFORMED, "srcCurMalformed", "Source currency is malformed."},
92  {rpcSRC_ISR_MALFORMED, "srcIsrMalformed", "Source issuer is malformed."},
93  {rpcSTREAM_MALFORMED, "malformedStream", "Stream malformed."},
94  {rpcTOO_BUSY, "tooBusy", "The server is too busy to help you now."},
95  {rpcTXN_NOT_FOUND, "txnNotFound", "Transaction not found."},
96  {rpcUNKNOWN_COMMAND, "unknownCmd", "Unknown method."},
97  {rpcSENDMAX_MALFORMED, "sendMaxMalformed", "SendMax amount malformed."},
98  {rpcOBJECT_NOT_FOUND, "objectNotFound", "The requested object was not found."}};
99 // clang-format on
100 
101 // C++ does not allow you to return an array from a function. You must
102 // return an object which may in turn contain an array. The following
103 // struct is simply defined so the enclosed array can be returned from a
104 // constexpr function.
105 //
106 // In C++17 this struct can be replaced by a std::array. But in C++14
107 // the constexpr methods of a std::array are not sufficient to perform the
108 // necessary work at compile time.
109 template <int N>
111 {
112  // Visual Studio doesn't treat a templated aggregate as an aggregate.
113  // So, for Visual Studio, we define a constexpr default constructor.
114  constexpr ErrorInfoArray() : infos{}
115  {
116  }
117 
119 };
120 
121 // Sort and validate unorderedErrorInfos at compile time. Should be
122 // converted to consteval when get to C++20.
123 template <int M, int N>
124 constexpr auto
125 sortErrorInfos(ErrorInfo const (&unordered)[N]) -> ErrorInfoArray<M>
126 {
127  ErrorInfoArray<M> ret;
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.infos[index].code != rpcUNKNOWN)
139  throw(std::invalid_argument("Duplicate error_code_i in list"));
140 
141  ret.infos[index].code = info.code;
142  ret.infos[index].token = info.token;
143  ret.infos[index].message = info.message;
144  }
145 
146  // Verify that all entries are filled in starting with 1 and proceeding
147  // to rpcLAST.
148  //
149  // It's okay for there to be missing entries; they will contain the code
150  // rpcUNKNOWN. But other than that all entries should match their index.
151  int codeCount{0};
152  int expect{rpcBAD_SYNTAX - 1};
153  for (ErrorInfo const& info : ret.infos)
154  {
155  ++expect;
156  if (info.code == rpcUNKNOWN)
157  continue;
158 
159  if (info.code != expect)
160  throw(std::invalid_argument("Empty error_code_i in list"));
161  ++codeCount;
162  }
163  if (expect != rpcLAST)
164  throw(std::invalid_argument("Insufficient list entries"));
165  if (codeCount != N)
166  throw(std::invalid_argument("Bad handling of unorderedErrorInfos"));
167 
168  return ret;
169 }
170 
171 constexpr auto sortedErrorInfos{sortErrorInfos<rpcLAST>(unorderedErrorInfos)};
172 
174 
175 } // namespace detail
176 
177 //------------------------------------------------------------------------------
178 
179 ErrorInfo const&
181 {
182  if (code <= rpcSUCCESS || code > rpcLAST)
183  return detail::unknownError;
184  return detail::sortedErrorInfos.infos[code - 1];
185 }
186 
189 {
190  Json::Value json;
191  inject_error(code, json);
192  return json;
193 }
194 
196 make_error(error_code_i code, std::string const& message)
197 {
198  Json::Value json;
199  inject_error(code, message, json);
200  return json;
201 }
202 
203 bool
205 {
206  if (json.isObject() && json.isMember(jss::error))
207  return true;
208  return false;
209 }
210 
211 } // namespace RPC
212 
215 {
216  assert(RPC::contains_error(jv));
217  return jv[jss::error].asString() + jv[jss::error_message].asString();
218 }
219 
220 } // namespace ripple
ripple::rpcNO_NETWORK
@ rpcNO_NETWORK
Definition: ErrorCodes.h:66
ripple::rpcDB_DESERIALIZATION
@ rpcDB_DESERIALIZATION
Definition: ErrorCodes.h:134
ripple::rpcDST_AMT_MALFORMED
@ rpcDST_AMT_MALFORMED
Definition: ErrorCodes.h:106
ripple::rpcNOT_SUPPORTED
@ rpcNOT_SUPPORTED
Definition: ErrorCodes.h:132
ripple::rpcLGR_IDXS_INVALID
@ rpcLGR_IDXS_INVALID
Definition: ErrorCodes.h:112
Json::Value::isObject
bool isObject() const
Definition: json_value.cpp:1027
std::string
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
ripple::RPC::get_error_info
ErrorInfo const & get_error_info(error_code_i code)
Returns an ErrorInfo that reflects the error code.
Definition: ErrorCodes.cpp:180
ripple::rpcErrorString
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
Definition: ErrorCodes.cpp:214
ripple::RPC::detail::sortedErrorInfos
constexpr auto sortedErrorInfos
Definition: ErrorCodes.cpp:171
ripple::rpcSRC_ACT_NOT_FOUND
@ rpcSRC_ACT_NOT_FOUND
Definition: ErrorCodes.h:122
ripple::rpcJSON_RPC
@ rpcJSON_RPC
Definition: ErrorCodes.h:47
ripple::rpcSIGNING_MALFORMED
@ rpcSIGNING_MALFORMED
Definition: ErrorCodes.h:118
ripple::rpcNOT_READY
@ rpcNOT_READY
Definition: ErrorCodes.h:60
ripple::rpcEXCESSIVE_LGR_RANGE
@ rpcEXCESSIVE_LGR_RANGE
Definition: ErrorCodes.h:135
ripple::RPC::detail::ErrorInfoArray::infos
ErrorInfo infos[N]
Definition: ErrorCodes.cpp:118
ripple::rpcTOO_BUSY
@ rpcTOO_BUSY
Definition: ErrorCodes.h:56
ripple::rpcATX_DEPRECATED
@ rpcATX_DEPRECATED
Definition: ErrorCodes.h:127
ripple::rpcLGR_NOT_FOUND
@ rpcLGR_NOT_FOUND
Definition: ErrorCodes.h:72
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::rpcOBJECT_NOT_FOUND
@ rpcOBJECT_NOT_FOUND
Definition: ErrorCodes.h:143
ripple::rpcAMENDMENT_BLOCKED
@ rpcAMENDMENT_BLOCKED
Definition: ErrorCodes.h:61
stdexcept
ripple::rpcSUCCESS
@ rpcSUCCESS
Definition: ErrorCodes.h:44
ripple::rpcSLOW_DOWN
@ rpcSLOW_DOWN
Definition: ErrorCodes.h:57
ripple::RPC::contains_error
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:204
ripple::rpcSRC_ACT_MISSING
@ rpcSRC_ACT_MISSING
Definition: ErrorCodes.h:121
ripple::rpcALREADY_SINGLE_SIG
@ rpcALREADY_SINGLE_SIG
Definition: ErrorCodes.h:92
ripple::rpcBAD_SEED
@ rpcBAD_SEED
Definition: ErrorCodes.h:99
ripple::rpcHIGH_FEE
@ rpcHIGH_FEE
Definition: ErrorCodes.h:58
ripple::rpcPUBLIC_MALFORMED
@ rpcPUBLIC_MALFORMED
Definition: ErrorCodes.h:117
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::rpcTXN_NOT_FOUND
@ rpcTXN_NOT_FOUND
Definition: ErrorCodes.h:80
ripple::rpcSRC_ISR_MALFORMED
@ rpcSRC_ISR_MALFORMED
Definition: ErrorCodes.h:125
ripple::rpcSTREAM_MALFORMED
@ rpcSTREAM_MALFORMED
Definition: ErrorCodes.h:126
ripple::rpcNOT_ENABLED
@ rpcNOT_ENABLED
Definition: ErrorCodes.h:59
ripple::rpcNO_EVENTS
@ rpcNO_EVENTS
Definition: ErrorCodes.h:54
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::rpcNO_CURRENT
@ rpcNO_CURRENT
Definition: ErrorCodes.h:65
std::invalid_argument
STL class.
ripple::RPC::ErrorInfo
Maps an rpc error code to its token and default message.
Definition: ErrorCodes.h:167
ripple::rpcDST_ISR_MALFORMED
@ rpcDST_ISR_MALFORMED
Definition: ErrorCodes.h:108
ripple::rpcCOMMAND_MISSING
@ rpcCOMMAND_MISSING
Definition: ErrorCodes.h:102
ripple::rpcDST_ACT_MALFORMED
@ rpcDST_ACT_MALFORMED
Definition: ErrorCodes.h:103
ripple::rpcALREADY_MULTISIG
@ rpcALREADY_MULTISIG
Definition: ErrorCodes.h:91
ripple::rpcLAST
@ rpcLAST
Definition: ErrorCodes.h:145
ripple::RPC::detail::unknownError
constexpr ErrorInfo unknownError
Definition: ErrorCodes.cpp:173
ripple::rpcINTERNAL
@ rpcINTERNAL
Definition: ErrorCodes.h:130
ripple::rpcCHANNEL_AMT_MALFORMED
@ rpcCHANNEL_AMT_MALFORMED
Definition: ErrorCodes.h:101
ripple::rpcDST_ACT_NOT_FOUND
@ rpcDST_ACT_NOT_FOUND
Definition: ErrorCodes.h:105
ripple::rpcNOT_IMPL
@ rpcNOT_IMPL
Definition: ErrorCodes.h:131
ripple::rpcSRC_ACT_MALFORMED
@ rpcSRC_ACT_MALFORMED
Definition: ErrorCodes.h:120
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::rpcFORBIDDEN
@ rpcFORBIDDEN
Definition: ErrorCodes.h:48
ripple::rpcACT_MALFORMED
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
ripple::rpcBAD_SYNTAX
@ rpcBAD_SYNTAX
Definition: ErrorCodes.h:46
ripple::rpcUNKNOWN_COMMAND
@ rpcUNKNOWN_COMMAND
Definition: ErrorCodes.h:85
ripple::rpcLGR_NOT_VALIDATED
@ rpcLGR_NOT_VALIDATED
Definition: ErrorCodes.h:73
ripple::rpcNO_PERMISSION
@ rpcNO_PERMISSION
Definition: ErrorCodes.h:53
cassert
ripple::rpcFAILED_TO_FORWARD
@ rpcFAILED_TO_FORWARD
Definition: ErrorCodes.h:140
ripple::rpcLGR_IDX_MALFORMED
@ rpcLGR_IDX_MALFORMED
Definition: ErrorCodes.h:113
ripple::rpcCHANNEL_MALFORMED
@ rpcCHANNEL_MALFORMED
Definition: ErrorCodes.h:100
ripple::rpcDST_AMT_MISSING
@ rpcDST_AMT_MISSING
Definition: ErrorCodes.h:107
ripple::rpcUNKNOWN
@ rpcUNKNOWN
Definition: ErrorCodes.h:42
std::out_of_range
STL class.
ripple::rpcBAD_MARKET
@ rpcBAD_MARKET
Definition: ErrorCodes.h:97
ripple::rpcINVALID_LGR_RANGE
@ rpcINVALID_LGR_RANGE
Definition: ErrorCodes.h:136
ripple::rpcSRC_CUR_MALFORMED
@ rpcSRC_CUR_MALFORMED
Definition: ErrorCodes.h:124
ripple::rpcDST_ACT_MISSING
@ rpcDST_ACT_MISSING
Definition: ErrorCodes.h:104
ripple::RPC::detail::sortErrorInfos
constexpr auto sortErrorInfos(ErrorInfo const (&unordered)[N]) -> ErrorInfoArray< M >
Definition: ErrorCodes.cpp:125
ripple::rpcNO_CLOSED
@ rpcNO_CLOSED
Definition: ErrorCodes.h:64
ripple::rpcBAD_SECRET
@ rpcBAD_SECRET
Definition: ErrorCodes.h:98
ripple::RPC::detail::ErrorInfoArray
Definition: ErrorCodes.cpp:110
ripple::rpcBAD_ISSUER
@ rpcBAD_ISSUER
Definition: ErrorCodes.h:96
ripple::RPC::detail::unorderedErrorInfos
constexpr static ErrorInfo unorderedErrorInfos[]
Definition: ErrorCodes.cpp:35
ripple::RPC::inject_error
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:196
ripple::RPC::detail::ErrorInfoArray::ErrorInfoArray
constexpr ErrorInfoArray()
Definition: ErrorCodes.cpp:114
ripple::rpcMASTER_DISABLED
@ rpcMASTER_DISABLED
Definition: ErrorCodes.h:74
ripple::RPC::make_error
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:188
ripple::rpcBAD_FEATURE
@ rpcBAD_FEATURE
Definition: ErrorCodes.h:95
ripple::rpcNO_PF_REQUEST
@ rpcNO_PF_REQUEST
Definition: ErrorCodes.h:86
ripple::rpcNOT_SYNCED
@ rpcNOT_SYNCED
Definition: ErrorCodes.h:67
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::rpcEXPIRED_VALIDATOR_LIST
@ rpcEXPIRED_VALIDATOR_LIST
Definition: ErrorCodes.h:137
ripple::rpcSENDMAX_MALFORMED
@ rpcSENDMAX_MALFORMED
Definition: ErrorCodes.h:119
ripple::RPC::ErrorInfo::code
error_code_i code
Definition: ErrorCodes.h:183
ripple::rpcBAD_KEY_TYPE
@ rpcBAD_KEY_TYPE
Definition: ErrorCodes.h:133
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469