rippled
ErrorCodes.h
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 #ifndef RIPPLE_PROTOCOL_ERRORCODES_H_INCLUDED
21 #define RIPPLE_PROTOCOL_ERRORCODES_H_INCLUDED
22 
23 #include <ripple/json/json_value.h>
24 #include <ripple/protocol/jss.h>
25 
26 namespace ripple {
27 
28 // VFALCO NOTE These are outside the RPC namespace
29 
30 // NOTE: Although the precise numeric values of these codes were never
31 // intended to be stable, several API endpoints include the numeric values.
32 // Some users came to rely on the values, meaning that renumbering would be
33 // a breaking change for those users.
34 //
35 // We therefore treat the range of values as stable although they are not
36 // and are subject to change.
37 //
38 // Please only append to this table. Do not "fill-in" gaps and do not re-use
39 // or repurpose error code values.
41  // -1 represents codes not listed in this enumeration
42  rpcUNKNOWN = -1,
43 
45 
49 
51  // Misc failure
52  // unused 5,
55  // unused 8,
62 
63  // Networking
68 
69  // Ledger state
71  // unused 20,
75  // unused 24,
76  // unused 25,
77  // unused 26,
78  // unused 27,
79  // unused 28,
82 
83  // Malformed command
87 
88  // Bad parameter
89  // NOT USED DO NOT USE AGAIN rpcACT_BITCOIN = 34,
93  // unused 38,
94  // unused 39,
109  // unused 54,
110  // unused 55,
111  // unused 56,
114  // unused 59,
115  // unused 60,
116  // unused 61,
123  // unused 68,
128 
129  // Internal error (should never happen)
130  rpcINTERNAL = 73, // Generic internal error.
138 
139  // Reporting
142 
144 
145  // AMM
147 
148  rpcLAST = rpcISSUE_MALFORMED // rpcLAST should always equal the last code.=
149 };
150 
160 };
161 
162 //------------------------------------------------------------------------------
163 
164 // VFALCO NOTE these should probably not be in the RPC namespace.
165 
166 namespace RPC {
167 
169 struct ErrorInfo
170 {
171  // Default ctor needed to produce an empty std::array during constexpr eval.
172  constexpr ErrorInfo()
173  : code(rpcUNKNOWN)
174  , token("unknown")
175  , message("An unknown error code.")
176  , http_status(200)
177  {
178  }
179 
180  constexpr ErrorInfo(
181  error_code_i code_,
182  char const* token_,
183  char const* message_)
184  : code(code_), token(token_), message(message_), http_status(200)
185  {
186  }
187 
188  constexpr ErrorInfo(
189  error_code_i code_,
190  char const* token_,
191  char const* message_,
192  int http_status_)
193  : code(code_)
194  , token(token_)
195  , message(message_)
196  , http_status(http_status_)
197  {
198  }
199 
204 };
205 
207 ErrorInfo const&
209 
212 template <class JsonValue>
213 void
214 inject_error(error_code_i code, JsonValue& json)
215 {
216  ErrorInfo const& info(get_error_info(code));
217  json[jss::error] = info.token;
218  json[jss::error_code] = info.code;
219  json[jss::error_message] = info.message;
220 }
221 
222 template <class JsonValue>
223 void
224 inject_error(int code, JsonValue& json)
225 {
226  inject_error(error_code_i(code), json);
227 }
228 
229 template <class JsonValue>
230 void
231 inject_error(error_code_i code, std::string const& message, JsonValue& json)
232 {
233  ErrorInfo const& info(get_error_info(code));
234  json[jss::error] = info.token;
235  json[jss::error_code] = info.code;
236  json[jss::error_message] = message;
237 }
238 
246 make_error(error_code_i code, std::string const& message);
251 inline Json::Value
253 {
254  return make_error(rpcINVALID_PARAMS, message);
255 }
256 
257 inline std::string
259 {
260  return "Missing field '" + name + "'.";
261 }
262 
263 inline Json::Value
265 {
267 }
268 
269 inline Json::Value
271 {
272  return missing_field_error(std::string(name));
273 }
274 
275 inline std::string
277 {
278  return "Invalid field '" + name + "', not object.";
279 }
280 
281 inline Json::Value
283 {
285 }
286 
287 inline Json::Value
289 {
290  return object_field_error(std::string(name));
291 }
292 
293 inline std::string
295 {
296  return "Invalid field '" + name + "'.";
297 }
298 
299 inline std::string
301 {
302  return invalid_field_message(std::string(name));
303 }
304 
305 inline Json::Value
307 {
309 }
310 
311 inline Json::Value
313 {
314  return invalid_field_error(std::string(name));
315 }
316 
317 inline std::string
319 {
320  return "Invalid field '" + name + "', not " + type + ".";
321 }
322 
323 inline std::string
325 {
326  return expected_field_message(std::string(name), type);
327 }
328 
329 inline Json::Value
331 {
332  return make_param_error(expected_field_message(name, type));
333 }
334 
335 inline Json::Value
337 {
338  return expected_field_error(std::string(name), type);
339 }
340 
341 inline Json::Value
343 {
344  return make_param_error("not a validator");
345 }
346 
350 bool
351 contains_error(Json::Value const& json);
352 
354 int
356 
357 } // namespace RPC
358 
361 rpcErrorString(Json::Value const& jv);
362 
363 } // namespace ripple
364 
365 #endif
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
std::string
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
ripple::RPC::ErrorInfo::http_status
int http_status
Definition: ErrorCodes.h:203
ripple::rpcWRONG_NETWORK
@ rpcWRONG_NETWORK
Definition: ErrorCodes.h:50
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:172
ripple::RPC::missing_field_message
std::string missing_field_message(std::string const &name)
Definition: ErrorCodes.h:258
ripple::rpcErrorString
std::string rpcErrorString(Json::Value const &jv)
Returns a single string with the contents of an RPC error.
Definition: ErrorCodes.cpp:212
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::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::RPC::object_field_error
Json::Value object_field_error(std::string const &name)
Definition: ErrorCodes.h:282
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::rpcOBJECT_NOT_FOUND
@ rpcOBJECT_NOT_FOUND
Definition: ErrorCodes.h:143
ripple::RPC::expected_field_error
Json::Value expected_field_error(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:330
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:264
ripple::rpcAMENDMENT_BLOCKED
@ rpcAMENDMENT_BLOCKED
Definition: ErrorCodes.h:61
ripple::RPC::ErrorInfo::message
Json::StaticString message
Definition: ErrorCodes.h:202
ripple::RPC::expected_field_message
std::string expected_field_message(std::string const &name, std::string const &type)
Definition: ErrorCodes.h:318
ripple::warnRPC_AMENDMENT_BLOCKED
@ warnRPC_AMENDMENT_BLOCKED
Definition: ErrorCodes.h:157
ripple::rpcSUCCESS
@ rpcSUCCESS
Definition: ErrorCodes.h:44
ripple::warnRPC_REPORTING
@ warnRPC_REPORTING
Definition: ErrorCodes.h:159
ripple::rpcSLOW_DOWN
@ rpcSLOW_DOWN
Definition: ErrorCodes.h:57
ripple::RPC::object_field_message
std::string object_field_message(std::string const &name)
Definition: ErrorCodes.h:276
ripple::RPC::ErrorInfo::ErrorInfo
constexpr ErrorInfo()
Definition: ErrorCodes.h:172
ripple::RPC::contains_error
bool contains_error(Json::Value const &json)
Returns true if the json contains an rpc error specification.
Definition: ErrorCodes.cpp:196
ripple::rpcSRC_ACT_MISSING
@ rpcSRC_ACT_MISSING
Definition: ErrorCodes.h:121
ripple::warning_code_i
warning_code_i
Codes returned in the warnings array of certain RPC commands.
Definition: ErrorCodes.h:155
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::warnRPC_EXPIRED_VALIDATOR_LIST
@ warnRPC_EXPIRED_VALIDATOR_LIST
Definition: ErrorCodes.h:158
ripple::rpcSTREAM_MALFORMED
@ rpcSTREAM_MALFORMED
Definition: ErrorCodes.h:126
ripple::rpcINVALID_HOTWALLET
@ rpcINVALID_HOTWALLET
Definition: ErrorCodes.h:81
ripple::warnRPC_UNSUPPORTED_MAJORITY
@ warnRPC_UNSUPPORTED_MAJORITY
Definition: ErrorCodes.h:156
ripple::rpcNOT_ENABLED
@ rpcNOT_ENABLED
Definition: ErrorCodes.h:59
ripple::rpcNO_EVENTS
@ rpcNO_EVENTS
Definition: ErrorCodes.h:54
ripple::rpcNO_CURRENT
@ rpcNO_CURRENT
Definition: ErrorCodes.h:65
ripple::RPC::ErrorInfo
Maps an rpc error code to its token, default message, and HTTP status.
Definition: ErrorCodes.h:169
ripple::rpcDST_ISR_MALFORMED
@ rpcDST_ISR_MALFORMED
Definition: ErrorCodes.h:108
ripple::rpcCOMMAND_MISSING
@ rpcCOMMAND_MISSING
Definition: ErrorCodes.h:102
ripple::RPC::not_validator_error
Json::Value not_validator_error()
Definition: ErrorCodes.h:342
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:148
ripple::RPC::ErrorInfo::ErrorInfo
constexpr ErrorInfo(error_code_i code_, char const *token_, char const *message_, int http_status_)
Definition: ErrorCodes.h:188
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::RPC::invalid_field_message
std::string invalid_field_message(std::string const &name)
Definition: ErrorCodes.h:294
ripple::rpcSRC_ACT_MALFORMED
@ rpcSRC_ACT_MALFORMED
Definition: ErrorCodes.h:120
ripple::RPC::error_code_http_status
int error_code_http_status(error_code_i code)
Returns http status that corresponds to the error code.
Definition: ErrorCodes.cpp:204
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
Json::StaticString
Lightweight wrapper to tag static string.
Definition: json_value.h:60
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
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::rpcISSUE_MALFORMED
@ rpcISSUE_MALFORMED
Definition: ErrorCodes.h:146
ripple::rpcDST_ACT_MISSING
@ rpcDST_ACT_MISSING
Definition: ErrorCodes.h:104
ripple::RPC::make_param_error
Json::Value make_param_error(std::string const &message)
Returns a new json object that indicates invalid parameters.
Definition: ErrorCodes.h:252
ripple::RPC::ErrorInfo::ErrorInfo
constexpr ErrorInfo(error_code_i code_, char const *token_, char const *message_)
Definition: ErrorCodes.h:180
ripple::rpcNO_CLOSED
@ rpcNO_CLOSED
Definition: ErrorCodes.h:64
ripple::rpcBAD_SECRET
@ rpcBAD_SECRET
Definition: ErrorCodes.h:98
ripple::rpcBAD_ISSUER
@ rpcBAD_ISSUER
Definition: ErrorCodes.h:96
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:214
ripple::RPC::invalid_field_error
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:306
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:180
ripple::rpcBAD_FEATURE
@ rpcBAD_FEATURE
Definition: ErrorCodes.h:95
ripple::RPC::ErrorInfo::token
Json::StaticString token
Definition: ErrorCodes.h:201
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:200
ripple::rpcBAD_KEY_TYPE
@ rpcBAD_KEY_TYPE
Definition: ErrorCodes.h:133