Skip to content

rpc: Return the right RPCErrorCode more consistently - #36826

Open
maflcko wants to merge 6 commits into
bitcoin:masterfrom
maflcko:2611-rpc-errors-consistent
Open

maflcko wants to merge 6 commits into
bitcoin:masterfrom
maflcko:2611-rpc-errors-consistent

Conversation

@maflcko

@maflcko maflcko commented Nov 10, 2026 •

Copy link
Copy Markdown
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

@DrahtBot

DrahtBot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

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 <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)
  • #36753 (rpc: Report -rpcwhitelist denials as JSON-RPC errors by ryanofsky)

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.

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>"}).

2026-11-12 13:50:40

@maflcko
maflcko force-pushed the 2611-rpc-errors-consistent branch from fa6e112 to fa99017 Compare November 11, 2026 12:55
@maflcko maflcko removed the CI failed label Nov 11, 2026
@maflcko
maflcko force-pushed the 2611-rpc-errors-consistent branch from fa99017 to fadce16 Compare November 11, 2026 13:03
@DrahtBot

Copy link
Copy Markdown
Contributor

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

Hints

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.

@maflcko
maflcko force-pushed the 2611-rpc-errors-consistent branch 2 times, most recently from 5555700 to faf54f9 Compare November 11, 2026 16:31
@DrahtBot

Copy link
Copy Markdown
Contributor

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

Hints

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.

@maflcko

maflcko commented Nov 11, 2026 •

Copy link
Copy Markdown
Member Author

(fixed ralph nit)

@Zippo76 Zippo76 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK faf54f9

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.

MarcoFalke added 6 commits November 12, 2026 14:48
…C_ERROR (-1)

This follows the other json type errors from commit
5556663.
Previously, the group comment was incorrectly attached to the first item
in the group.
This follows all other uses of the constant, like:

throw JSONRPCError(RPC_INVALID_PARAMETER, "The PSBT version can only be 2 or 0");
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...");
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.
This refactor does not change any behavior.
@maflcko
maflcko force-pushed the 2611-rpc-errors-consistent branch from faf54f9 to face783 Compare November 12, 2026 13:50
@maflcko

maflcko commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

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.

@Zippo76 Zippo76 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK face783

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants