Skip to content

Commit 5556663

Browse files
committed
rpc: treat univalue type check error as RPC_TYPE_ERROR, not RPC_MISC_ERROR
By throwing a custom exception from `Univalue::checkType` (instead of a plain std::runtime_error) and catching it on the RPC server request handler. So we properly return RPC_TYPE_ERROR (-3) on arg type errors and not the general RPC_MISC_ERROR (-1).
1 parent f523df1 commit 5556663

File tree

11 files changed

+28
-24
lines changed

11 files changed

+28
-24
lines changed

src/rpc/server.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,18 +468,17 @@ UniValue CRPCTable::execute(const JSONRPCRequest &request) const
468468

469469
static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& request, UniValue& result, bool last_handler)
470470
{
471-
try
472-
{
471+
try {
473472
RPCCommandExecution execution(request.strMethod);
474473
// Execute, convert arguments to array if necessary
475474
if (request.params.isObject()) {
476475
return command.actor(transformNamedArguments(request, command.argNames), result, last_handler);
477476
} else {
478477
return command.actor(request, result, last_handler);
479478
}
480-
}
481-
catch (const std::exception& e)
482-
{
479+
} catch (const UniValue::type_error& e) {
480+
throw JSONRPCError(RPC_TYPE_ERROR, e.what());
481+
} catch (const std::exception& e) {
483482
throw JSONRPCError(RPC_MISC_ERROR, e.what());
484483
}
485484
}

src/univalue/include/univalue.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class UniValue {
1919
public:
2020
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
2121

22+
class type_error : public std::runtime_error
23+
{
24+
using std::runtime_error::runtime_error;
25+
};
26+
2227
UniValue() { typ = VNULL; }
2328
UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
2429
typ = initialType;

src/univalue/lib/univalue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const UniValue& UniValue::operator[](size_t index) const
210210
void UniValue::checkType(const VType& expected) const
211211
{
212212
if (typ != expected) {
213-
throw std::runtime_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
213+
throw type_error{"JSON value of type " + std::string{uvTypeName(typ)} + " is not of expected type " +
214214
std::string{uvTypeName(expected)}};
215215
}
216216
}

test/functional/mining_prioritisetransaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ def run_test(self):
122122

123123
# Test `prioritisetransaction` invalid `dummy`
124124
txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
125-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid, 'foo', 0)
125+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid, 'foo', 0)
126126
assert_raises_rpc_error(-8, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.", self.nodes[0].prioritisetransaction, txid, 1, 0)
127127

128128
# Test `prioritisetransaction` invalid `fee_delta`
129-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo')
129+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo')
130130

131131
self.test_diamond()
132132

test/functional/rpc_blockchain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ def _test_getchaintxstats(self):
262262
assert_raises_rpc_error(-1, 'getchaintxstats', self.nodes[0].getchaintxstats, 0, '', 0)
263263

264264
# Test `getchaintxstats` invalid `nblocks`
265-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", self.nodes[0].getchaintxstats, '')
265+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", self.nodes[0].getchaintxstats, '')
266266
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, -1)
267267
assert_raises_rpc_error(-8, "Invalid block count: should be between 0 and the block's height - 1", self.nodes[0].getchaintxstats, self.nodes[0].getblockcount())
268268

269269
# Test `getchaintxstats` invalid `blockhash`
270-
assert_raises_rpc_error(-1, "JSON value of type number is not of expected type string", self.nodes[0].getchaintxstats, blockhash=0)
270+
assert_raises_rpc_error(-3, "JSON value of type number is not of expected type string", self.nodes[0].getchaintxstats, blockhash=0)
271271
assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
272272
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
273273
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
@@ -547,7 +547,7 @@ def assert_vin_does_not_contain_prevout(verbosity):
547547
datadir = get_datadir_path(self.options.tmpdir, 0)
548548

549549
self.log.info("Test getblock with invalid verbosity type returns proper error message")
550-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type number", node.getblock, blockhash, "2")
550+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type number", node.getblock, blockhash, "2")
551551

552552
def move_block_file(old, new):
553553
old_path = os.path.join(datadir, self.chain, 'blocks', old)

test/functional/rpc_fundrawtransaction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def test_change_type(self):
301301
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
302302
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
303303
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
304-
assert_raises_rpc_error(-1, "JSON value of type null is not of expected type string", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
304+
assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", self.nodes[2].fundrawtransaction, rawtx, {'change_type': None})
305305
assert_raises_rpc_error(-5, "Unknown change type ''", self.nodes[2].fundrawtransaction, rawtx, {'change_type': ''})
306306
rawtx = self.nodes[2].fundrawtransaction(rawtx, {'change_type': 'bech32'})
307307
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])

test/functional/rpc_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_categories(self):
9292
assert_raises_rpc_error(-1, 'help', node.help, 'foo', 'bar')
9393

9494
# invalid argument
95-
assert_raises_rpc_error(-1, "JSON value of type number is not of expected type string", node.help, 0)
95+
assert_raises_rpc_error(-3, "JSON value of type number is not of expected type string", node.help, 0)
9696

9797
# help of unknown command
9898
assert_equal(node.help('foo'), 'help: unknown command: foo')

test/functional/rpc_rawtransaction.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ def getrawtransaction_tests(self):
124124

125125
# 6. invalid parameters - supply txid and invalid boolean values (strings) for verbose
126126
for value in ["True", "False"]:
127-
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txid=txId, verbose=value)
127+
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txid=txId, verbose=value)
128128

129129
# 7. invalid parameters - supply txid and empty array
130-
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txId, [])
130+
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txId, [])
131131

132132
# 8. invalid parameters - supply txid and empty dict
133-
assert_raises_rpc_error(-1, "not of expected type bool", self.nodes[n].getrawtransaction, txId, {})
133+
assert_raises_rpc_error(-3, "not of expected type bool", self.nodes[n].getrawtransaction, txId, {})
134134

135135
# Make a tx by sending, then generate 2 blocks; block1 has the tx in it
136136
tx = self.wallet.send_self_transfer(from_node=self.nodes[2])['txid']
@@ -152,7 +152,7 @@ def getrawtransaction_tests(self):
152152
# We should not get the tx if we provide an unrelated block
153153
assert_raises_rpc_error(-5, "No such transaction found", self.nodes[n].getrawtransaction, txid=tx, blockhash=block2)
154154
# An invalid block hash should raise the correct errors
155-
assert_raises_rpc_error(-1, "JSON value of type bool is not of expected type string", self.nodes[n].getrawtransaction, txid=tx, blockhash=True)
155+
assert_raises_rpc_error(-3, "JSON value of type bool is not of expected type string", self.nodes[n].getrawtransaction, txid=tx, blockhash=True)
156156
assert_raises_rpc_error(-8, "parameter 3 must be of length 64 (not 6, for 'foobar')", self.nodes[n].getrawtransaction, txid=tx, blockhash="foobar")
157157
assert_raises_rpc_error(-8, "parameter 3 must be of length 64 (not 8, for 'abcd1234')", self.nodes[n].getrawtransaction, txid=tx, blockhash="abcd1234")
158158
foo = "ZZZ0000000000000000000000000000000000000000000000000000000000000"
@@ -181,8 +181,8 @@ def createrawtransaction_tests(self):
181181

182182
# Test `createrawtransaction` invalid `inputs`
183183
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type array", self.nodes[0].createrawtransaction, 'foo', {})
184-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type object", self.nodes[0].createrawtransaction, ['foo'], {})
185-
assert_raises_rpc_error(-1, "JSON value of type null is not of expected type string", self.nodes[0].createrawtransaction, [{}], {})
184+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type object", self.nodes[0].createrawtransaction, ['foo'], {})
185+
assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", self.nodes[0].createrawtransaction, [{}], {})
186186
assert_raises_rpc_error(-8, "txid must be of length 64 (not 3, for 'foo')", self.nodes[0].createrawtransaction, [{'txid': 'foo'}], {})
187187
txid = "ZZZ7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844"
188188
assert_raises_rpc_error(-8, f"txid must be hexadecimal string (not '{txid}')", self.nodes[0].createrawtransaction, [{'txid': txid}], {})
@@ -207,7 +207,7 @@ def createrawtransaction_tests(self):
207207

208208
# Test `createrawtransaction` invalid `outputs`
209209
address = getnewdestination()[2]
210-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type array", self.nodes[0].createrawtransaction, [], 'foo')
210+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type array", self.nodes[0].createrawtransaction, [], 'foo')
211211
self.nodes[0].createrawtransaction(inputs=[], outputs={}) # Should not throw for backwards compatibility
212212
self.nodes[0].createrawtransaction(inputs=[], outputs=[])
213213
assert_raises_rpc_error(-8, "Data must be hexadecimal string", self.nodes[0].createrawtransaction, [], {'data': 'foo'})

test/functional/wallet_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def run_test(self):
415415
assert_raises_rpc_error(-3, "Invalid amount", self.nodes[0].sendtoaddress, self.nodes[2].getnewaddress(), "1f-4")
416416

417417
# This will raise an exception since generate does not accept a string
418-
assert_raises_rpc_error(-1, "not of expected type number", self.generate, self.nodes[0], "2")
418+
assert_raises_rpc_error(-3, "not of expected type number", self.generate, self.nodes[0], "2")
419419

420420
if not self.options.descriptors:
421421

test/functional/wallet_hd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def run_test(self):
177177
# Sethdseed parameter validity
178178
assert_raises_rpc_error(-1, 'sethdseed', self.nodes[0].sethdseed, False, new_seed, 0)
179179
assert_raises_rpc_error(-5, "Invalid private key", self.nodes[1].sethdseed, False, "not_wif")
180-
assert_raises_rpc_error(-1, "JSON value of type string is not of expected type bool", self.nodes[1].sethdseed, "Not_bool")
181-
assert_raises_rpc_error(-1, "JSON value of type bool is not of expected type string", self.nodes[1].sethdseed, False, True)
180+
assert_raises_rpc_error(-3, "JSON value of type string is not of expected type bool", self.nodes[1].sethdseed, "Not_bool")
181+
assert_raises_rpc_error(-3, "JSON value of type bool is not of expected type string", self.nodes[1].sethdseed, False, True)
182182
assert_raises_rpc_error(-5, "Already have this key", self.nodes[1].sethdseed, False, new_seed)
183183
assert_raises_rpc_error(-5, "Already have this key", self.nodes[1].sethdseed, False, self.nodes[1].dumpprivkey(self.nodes[1].getnewaddress()))
184184

0 commit comments

Comments
 (0)