Skip to content

Commit 48174c0

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#26240: rpc: Adjust RPCTypeCheckObj error string
2dede9f Adjust RPCTypeCheckObj error string (Leonardo Araujo) Pull request description: Unifies the JSON type error strings as mentioned in #26214. Also refer to #25737. ACKs for top commit: furszy: ACK 2dede9f Tree-SHA512: c918889e347ba32cb6d0e33c0de5956c2077dd40c996151e16741b0c4983ff098c60258206ded76ad7bbec4876c780c6abb494a97e4f1e05717d28a59b9167a6
2 parents 59e00c7 + 2dede9f commit 48174c0

File tree

6 files changed

+9
-12
lines changed

6 files changed

+9
-12
lines changed

src/rpc/util.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ void RPCTypeCheckObj(const UniValue& o,
6565
if (!fAllowNull && v.isNull())
6666
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
6767

68-
if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull()))) {
69-
std::string err = strprintf("Expected type %s for %s, got %s",
70-
uvTypeName(t.second.type), t.first, uvTypeName(v.type()));
71-
throw JSONRPCError(RPC_TYPE_ERROR, err);
72-
}
68+
if (!(t.second.typeAny || v.type() == t.second.type || (fAllowNull && v.isNull())))
69+
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("JSON value of type %s for field %s is not of expected type %s", uvTypeName(v.type()), t.first, uvTypeName(t.second.type)));
7370
}
7471

7572
if (fStrict)

test/functional/rpc_fundrawtransaction.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ def test_option_feerate(self):
795795

796796
self.log.info("Test fundrawtxn with invalid estimate_mode settings")
797797
for k, v in {"number": 42, "object": {"foo": "bar"}}.items():
798-
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
798+
assert_raises_rpc_error(-3, f"JSON value of type {k} for field estimate_mode is not of expected type string",
799799
node.fundrawtransaction, rawtx, {"estimate_mode": v, "conf_target": 0.1, "add_inputs": True})
800800
for mode in ["", "foo", Decimal("3.141592")]:
801801
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
@@ -805,7 +805,7 @@ def test_option_feerate(self):
805805
for mode in ["unset", "economical", "conservative"]:
806806
self.log.debug("{}".format(mode))
807807
for k, v in {"string": "", "object": {"foo": "bar"}}.items():
808-
assert_raises_rpc_error(-3, "Expected type number for conf_target, got {}".format(k),
808+
assert_raises_rpc_error(-3, f"JSON value of type {k} for field conf_target is not of expected type number",
809809
node.fundrawtransaction, rawtx, {"estimate_mode": mode, "conf_target": v, "add_inputs": True})
810810
for n in [-1, 0, 1009]:
811811
assert_raises_rpc_error(-8, "Invalid conf_target, must be between 1 and 1008", # max value of 1008 per src/policy/fees.h

test/functional/rpc_mempool_info.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def create_tx(**kwargs):
8484
assert_raises_rpc_error(-8, "Invalid parameter, vout cannot be negative", self.nodes[0].gettxspendingprevout, [{'txid' : txidA, 'vout' : -1}])
8585

8686
self.log.info("Invalid txid provided")
87-
assert_raises_rpc_error(-3, "Expected type string for txid, got number", self.nodes[0].gettxspendingprevout, [{'txid' : 42, 'vout' : 0}])
87+
assert_raises_rpc_error(-3, "JSON value of type number for field txid is not of expected type string", self.nodes[0].gettxspendingprevout, [{'txid' : 42, 'vout' : 0}])
8888

8989
self.log.info("Missing outputs")
9090
assert_raises_rpc_error(-8, "Invalid parameter, outputs are missing", self.nodes[0].gettxspendingprevout, [])

test/functional/rpc_psbt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def run_test(self):
275275

276276
self.log.info("- raises RPC error with invalid estimate_mode settings")
277277
for k, v in {"number": 42, "object": {"foo": "bar"}}.items():
278-
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
278+
assert_raises_rpc_error(-3, f"JSON value of type {k} for field estimate_mode is not of expected type string",
279279
self.nodes[1].walletcreatefundedpsbt, inputs, outputs, 0, {"estimate_mode": v, "conf_target": 0.1, "add_inputs": True})
280280
for mode in ["", "foo", Decimal("3.141592")]:
281281
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',
@@ -285,7 +285,7 @@ def run_test(self):
285285
for mode in ["unset", "economical", "conservative"]:
286286
self.log.debug("{}".format(mode))
287287
for k, v in {"string": "", "object": {"foo": "bar"}}.items():
288-
assert_raises_rpc_error(-3, "Expected type number for conf_target, got {}".format(k),
288+
assert_raises_rpc_error(-3, f"JSON value of type {k} for field conf_target is not of expected type number",
289289
self.nodes[1].walletcreatefundedpsbt, inputs, outputs, 0, {"estimate_mode": mode, "conf_target": v, "add_inputs": True})
290290
for n in [-1, 0, 1009]:
291291
assert_raises_rpc_error(-8, "Invalid conf_target, must be between 1 and 1008", # max value of 1008 per src/policy/fees.h

test/functional/wallet_bumpfee.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_invalid_parameters(self, rbf_node, peer_node, dest_address):
151151

152152
self.log.info("Test invalid estimate_mode settings")
153153
for k, v in {"number": 42, "object": {"foo": "bar"}}.items():
154-
assert_raises_rpc_error(-3, "Expected type string for estimate_mode, got {}".format(k),
154+
assert_raises_rpc_error(-3, f"JSON value of type {k} for field estimate_mode is not of expected type string",
155155
rbf_node.bumpfee, rbfid, {"estimate_mode": v})
156156
for mode in ["foo", Decimal("3.1415"), "sat/B", "BTC/kB"]:
157157
assert_raises_rpc_error(-8, 'Invalid estimate_mode parameter, must be one of: "unset", "economical", "conservative"',

test/functional/wallet_send.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def run_test(self):
362362
for mode in ["economical", "conservative"]:
363363
for k, v in {"string": "true", "bool": True, "object": {"foo": "bar"}}.items():
364364
self.test_send(from_wallet=w0, to_wallet=w1, amount=1, conf_target=v, estimate_mode=mode,
365-
expect_error=(-3, f"Expected type number for conf_target, got {k}"))
365+
expect_error=(-3, f"JSON value of type {k} for field conf_target is not of expected type number"))
366366

367367
# Test setting explicit fee rate just below the minimum of 1 sat/vB.
368368
self.log.info("Explicit fee rate raises RPC error 'fee rate too low' if fee_rate of 0.99999999 is passed")

0 commit comments

Comments
 (0)