rpc: Return the right RPCErrorCode more consistently #36826

pull maflcko wants to merge 6 commits into bitcoin:master from maflcko:2611-rpc-errors-consistent changing 13 files +58 −41
  1. maflcko commented at 1:20 PM on November 10, 2026: member

    Currently, rpc code may return the wrong RPCErrorCode. This is perfectly fine, because they should be hit rarely in production.

    However, it could be good to be self-consistent. (Otherwise, we could just remove the different error codes and return a single one)

    So fix a few error codes:

    • An integral out-of-range is now RPC_TYPE_ERROR, not RPC_MISC_ERROR
    • Some invalid args are now RPC_INVALID_PARAMETER
    • Some transient issues or user-config issues are now RPC_MISC_ERROR
  2. DrahtBot added the label RPC/REST/ZMQ on Nov 10, 2026
  3. DrahtBot commented at 1:20 PM on November 10, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK Zippo76

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36990 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36990.svg"></sub> (test: replace hard-coded RPC error codes by l0rinc)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    LLM Linter (✨ experimental)

    Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

    • test/functional/feature_fee_estimation.py: assert_raises_rpc_error(-1, "Fee estimation disabled", self.nodes[0].estimatesmartfee, 2)

    Possible places where comparison-specific test macros should replace generic comparisons:

    • src/univalue/test/object.cpp: BOOST_CHECK_THROW(v_negative.getInt<uint8_t>(), UniValue::type_error) → Prefer BOOST_CHECK_EXCEPTION(..., UniValue::type_error, HasReason{"JSON integer out of range"}).
    • src/univalue/test/object.cpp: BOOST_CHECK_THROW(v2.getInt<int>(), UniValue::type_error) → Prefer BOOST_CHECK_EXCEPTION(..., UniValue::type_error, HasReason{"<expected type-error message>"}).
    • src/univalue/test/object.cpp: BOOST_CHECK_THROW(v3.getInt<int64_t>(), UniValue::type_error) → Prefer BOOST_CHECK_EXCEPTION(..., UniValue::type_error, HasReason{"JSON integer out of range"}).
    • src/univalue/test/object.cpp: BOOST_CHECK_THROW(v4.getInt<int>(), UniValue::type_error) → Prefer BOOST_CHECK_EXCEPTION(..., UniValue::type_error, HasReason{"JSON integer out of range"}).
    • src/univalue/test/object.cpp: BOOST_CHECK_THROW(vals[0].getInt<int>(), UniValue::type_error) → Prefer BOOST_CHECK_EXCEPTION(..., UniValue::type_error, HasReason{"<expected type-error message>"}).

    <sup>2026-11-12 13:50:40</sup>

  4. DrahtBot added the label CI failed on Nov 10, 2026
  5. maflcko force-pushed on Nov 11, 2026
  6. maflcko removed the label CI failed on Nov 11, 2026
  7. maflcko force-pushed on Nov 11, 2026
  8. DrahtBot added the label CI failed on Nov 11, 2026
  9. DrahtBot commented at 1:04 PM on November 11, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/43024502508/job/130535654515</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU/clang-format modified src/rpc/mining.cpp, causing the clean-diff check to fail.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  10. DrahtBot removed the label CI failed on Nov 11, 2026
  11. maflcko force-pushed on Nov 11, 2026
  12. maflcko force-pushed on Nov 11, 2026
  13. DrahtBot added the label CI failed on Nov 11, 2026
  14. DrahtBot commented at 4:32 PM on November 11, 2026: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 At least one of the CI tasks failed. <sub>Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/43054999350/job/130641963665</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU modified includes in src/rpc/external_signer.cpp, leaving a diff that the formatting/lint check rejects.</sub>

    <details><summary>Hints</summary>

    Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example:

    • Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    • A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test.

    • An intermittent issue.

    Leave a comment here, if you need help tracking down a confusing failure.

    </details>

  15. maflcko commented at 4:38 PM on November 11, 2026: member

    (fixed ralph nit)

    <!-- diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 20a5a3f3fc..c5ebdce25a 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -196,6 +196,16 @@ class WalletSignerTest(BitcoinTestFramework): assert res["complete"] assert_equal(res["hex"], mock_tx) + self.log.info('Test external signer signing failure') + self.set_mock_result(self.nodes[1], "2") + assert_raises_rpc_error( + -25, + "External signer failed to sign", + hww.send, + outputs={dest: 0.5}, add_to_wallet=False, + ) + self.clear_mock_result(self.nodes[1]) + self.log.info('Test sendall using hww1') res = hww.sendall(recipients=[{dest:0.5}, hww.getrawchangeaddress()], add_to_wallet=False) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a6bbc30..9490077 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -3557,3 +3557,3 @@ static RPCMethod loadtxoutset() if (!activation_result) { - throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot: %s. (%s)", util::ErrorString(activation_result).original, path.utf8string())); + throw JSONRPCError(RPC_MISC_ERROR, strprintf("Unable to load UTXO snapshot: %s. (%s)", util::ErrorString(activation_result).original, path.utf8string())); } diff --git a/test/functional/feature_assumeutxo.py b/test/functional/feature_assumeutxo.py index fa9466f..800826b 100755 --- a/test/functional/feature_assumeutxo.py +++ b/test/functional/feature_assumeutxo.py @@ -87,3 +87,3 @@ class AssumeutxoTest(BitcoinTestFramework): def expected_error(msg): - assert_raises_rpc_error(-32603, f"Unable to load UTXO snapshot: Population failed: {msg}", node.loadtxoutset, bad_snapshot_path) + assert_raises_rpc_error(-1, f"Unable to load UTXO snapshot: Population failed: {msg}", node.loadtxoutset, bad_snapshot_path) @@ -127,3 +127,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = f"Unable to load UTXO snapshot: assumeutxo block hash in snapshot metadata not recognized (hash: {bad_block_hash}). The following snapshot heights are available: 110, 200, 299." - assert_raises_rpc_error(-32603, msg, node.loadtxoutset, bad_snapshot_path) + assert_raises_rpc_error(-1, msg, node.loadtxoutset, bad_snapshot_path) @@ -171,3 +171,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "Unable to load UTXO snapshot: The base block header (0c552ced4721c249a389eb9b08cb8da261cd46f0e7b5f9d064d48f3113406853) must appear in the headers chain. Make sure all headers are syncing, and call loadtxoutset again." - assert_raises_rpc_error(-32603, msg, node.loadtxoutset, valid_snapshot_path) + assert_raises_rpc_error(-1, msg, node.loadtxoutset, valid_snapshot_path) @@ -205,3 +205,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "Unable to load UTXO snapshot: Can't activate a snapshot when mempool not empty" - assert_raises_rpc_error(-32603, msg, node.loadtxoutset, dump_output_path) + assert_raises_rpc_error(-1, msg, node.loadtxoutset, dump_output_path) @@ -219,3 +219,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "Unable to load UTXO snapshot: Population failed: Work does not exceed active chainstate." - assert_raises_rpc_error(-32603, msg, node.loadtxoutset, dump_output_path) + assert_raises_rpc_error(-1, msg, node.loadtxoutset, dump_output_path) @@ -231,3 +231,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "Unable to load UTXO snapshot: The base block header (0c552ced4721c249a389eb9b08cb8da261cd46f0e7b5f9d064d48f3113406853) is part of an invalid chain." - assert_raises_rpc_error(-32603, msg, node.loadtxoutset, dump_output_path) + assert_raises_rpc_error(-1, msg, node.loadtxoutset, dump_output_path) node.reconsiderblock(block_hash) @@ -279,3 +279,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "A forked headers-chain with more work than the chain with the snapshot base block header exists. Please proceed to sync without AssumeUtxo." - assert_raises_rpc_error(-32603, msg, node1.loadtxoutset, dump_output_path) + assert_raises_rpc_error(-1, msg, node1.loadtxoutset, dump_output_path) # Cleanup: submit two more headers of the snapshot chain to node 1, so that it is the most-work chain again and loading @@ -756,3 +756,3 @@ class AssumeutxoTest(BitcoinTestFramework): msg = "Unable to load UTXO snapshot: Can't activate a snapshot-based chainstate more than once" - assert_raises_rpc_error(-32603, msg, n2.loadtxoutset, dump_output['path']) + assert_raises_rpc_error(-1, msg, n2.loadtxoutset, dump_output['path'])

  16. DrahtBot removed the label CI failed on Nov 11, 2026
  17. Zippo76 commented at 8:03 AM on November 12, 2026: none

    tACK faf54f9c1b

    Tested on macOS 27.2 arm64 (release build) against master c68583ab5a. The PR's rpc_net.py fails on master with Unexpected JSONRPC error code -1 and passes with this PR.

    Case Calls tested Master PR
    Integer out of range getblockhash 99999999999999999999, getblockhash -99999999999999999999, listtransactions "*" -1, addpeeraddress 1.2.3.4 65536 -1 -3
    Invalid argument addnode 127.0.0.1:1 abc, setban 1.2.3.4 abc, mockscheduler 0 -1 -8
    Simulated internal bug -named echo arg9=trigger_internal_bug -1 -32603
    Fee estimation disabled (-blocksonly) estimatesmartfee 2, estimaterawfee 2 -32603 -1
    Missing argument getblockhash -1 -1
    Height out of range getblockhash 1000 -8 -8
    Invalid block hash getblock abc -8 -8
    Unknown method Nonexistent RPC method -32601 -32601
    Help help getblockhash OK OK

    Error messages were identical before and after in all tested error cases; only the codes changed where shown. The node remained running after the simulated internal bug in this release build.

  18. rpc: For numeric out-of-range, throw RPC_TYPE_ERROR (-3), not RPC_MISC_ERROR (-1)
    This follows the other json type errors from commit
    55566630c60d23993a52ed54c95e7891f4588d57.
    7777449c96
  19. doc: Use proper doxygen groups for RPCErrorCode
    Previously, the group comment was incorrectly attached to the first item
    in the group.
    faa1b0dfa4
  20. rpc: Use RPC_INVALID_PARAMETER for invalid args
    This follows all other uses of the constant, like:
    
    throw JSONRPCError(RPC_INVALID_PARAMETER, "The PSBT version can only be 2 or 0");
    fa72788687
  21. rpc: Use RPC_INTERNAL_ERROR for unreachable internal error only
    Also, switch the two violations to use RPC_MISC_ERROR (-1):
    
    * coinstatsindex still syncing
    * disabled fee estimation
    
    This follows other intermittent or user-config errors, such as:
    
    * throw JSONRPCError(RPC_MISC_ERROR, "The mempool was not loaded yet");
    * throw JSONRPCError(RPC_MISC_ERROR, "Can't rescan beyond pruned data...");
    fa19dc6fed
  22. rpc: refactor: Use explicit RPC_MISC_ERROR
    This refactor does not change any behavior because a runtime_error
    exception is currently mapped to RPC_MISC_ERROR.
    
    Though, using explicit RPC_MISC_ERROR is clearer and follows other
    intermittent or user-config errors.
    fa73b49bb8
  23. rpc: refactor: Explicitly throw rpc help as RPC_MISC_ERROR (-1)
    This refactor does not change any behavior.
    face7839be
  24. maflcko force-pushed on Nov 12, 2026
  25. maflcko commented at 1:50 PM on November 12, 2026: member

    Hmm, I dropped the last three commits on my last push. Maybe they can be done in the future, but for now they are likely not worth the squeeze, as they are ~impossible to review.

  26. Zippo76 commented at 8:36 PM on November 12, 2026: none

    tACK face7839be

    Repeated all checks from my previous review on macOS 27.2.1 arm64 against master 9183edd25e, after the last three commits were removed. Results are unchanged except:

    • -named echo arg9=trigger_internal_bug now returns -1 on both master and this PR, consistent with the removed commits.
    • The additional check estimaterawfee 2 1e999 ("JSON double out of range") returns -1 on master and -3 with this PR.

    The PR's rpc_net.py still fails on master and passes with this PR.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-01 02:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me