rpc: Return the right RPCErrorCode more consistently

bitcoin/bitcoin: PR #36826

2 minor findings · 3 suggestions

Findings

Link

[minor] · Malformed RPC inputs are reported as internal errors

Overflowing doubles, truncated Merkle proofs and oversized wallet passphrases can now return -32603 instead of -1, misclassifying authenticated clients' invalid input as an internal bug.

Location: src/rpc/server.cpp:893 (head).

Details and suggested fix

UniValue::get_real still throws plain std::runtime_error on overflow. For example, prioritisetransaction <valid-txid> 1e400 0 reaches it before validating dummy. The same applies to the threshold in estimaterawfee when fee estimation is enabled. Throw UniValue::type_error here, matching the integer conversion change.

Both verifytxoutproof and importprunedfunds deserialize caller-supplied proofs without translating SpanReader short-read exceptions. For the latter, a selected wallet and decodable transaction are sufficient to reach the decode. Catch std::ios_base::failure around these decodes and return RPC_DESERIALIZATION_ERROR.

Once the wallet-state guards pass, a passphrase of at least 256 KiB in encryptwallet, walletpassphrase or walletpassphrasechange requires a secure allocation larger than LockedPool::ARENA_SIZE. The allocator explicitly throws std::bad_alloc for that size limit, even without memory exhaustion. Reject oversized passphrases with RPC_INVALID_PARAMETER before copying them into SecureString. Add functional cases for these input-triggered error paths.


Link

[minor] · dumptxoutset classifies output failures inconsistently

Escaping snapshot-write and filesystem exceptions now map to -32603 while close failures remain -1, so an ordinary output-volume failure can be reported as an internal bug.

Location: src/rpc/blockchain.cpp:3467 (head).

Details and suggested fix

A full output volume can cause a short fwrite, making AutoFile::write throw std::ios_base::failure. The metadata and coin writes have no local exception translation, unlike the explicit RPC_MISC_ERROR handling at fclose. Output-path operations such as the final fs::rename, and temporary-directory creation during rollback with in_memory=false, also lack translation for fs::filesystem_error. These escaping exceptions returned -1 at base. Translate snapshot-output I/O and output-path filesystem exceptions to RPC_MISC_ERROR, while leaving the coin-count CHECK_NONFATAL on the internal-error path. Add coverage for a failure detected during writing rather than closing.


Optional improvements

Link

[suggestion] · Signer exception details no longer reach RPC or GUI users

Newly caught signer subprocess exceptions lose their detailed RPC and GUI messages, leaving users with a generic failure and the diagnostic only in the log.

Location: src/wallet/external_signer_scriptpubkeyman.cpp:113 (head).

Details and suggested fix

For an incomplete PSBT requiring external signing, a nonzero subprocess exit or invalid JSON previously escaped FillPSBT with diagnostic text, including stderr for a nonzero exit. This catch logs that text and returns only EXTERNAL_SIGNER_FAILED. For example, walletprocesspsbt now returns -25 with "External signer failed to sign", and the GUI shows "External signer failure" instead of e.what(). Enumeration exceptions also enter this catch. Preserve the structured error but carry its reason alongside the enum, for example through util::Result, so RPC and GUI callers can display it. This loss concerns newly intercepted exceptions; signer failures returned as false already had generic user-facing messages.


Link

[suggestion] · Client-visible RPC error-code changes have no release note

Clients that branch on numeric RPC codes receive undocumented changes, including the generic exception fallback moving from -1 to -32603.

Location: src/rpc/server.cpp:893 (head).

Details and suggested fix

Add an RPC release-note fragment covering integer range errors changing from -1 to -3; invalid addnode/setban commands and out-of-range mockscheduler intervals changing from -1 to -8; disabled fee estimation and a still-syncing coinstats index changing from -32603 to -1; and otherwise unhandled exceptions changing from -1 to -32603, excluding the preserved type-error and help handling. For example, clients matching the old code for estimatesmartfee under -blocksonly need to update that match. doc/developer-notes.md requests release notes for API changes, and doc/release-notes-34861.md provides an error-code-change example.


Link

[suggestion] · Generate RPCs classify temporary template failures as internal errors

When timestamp constraints make template creation fail, the generate RPCs now return -32603 instead of -1, unlike getblocktemplate's explicit handling of this temporary condition.

Location: src/rpc/mining.cpp:913 (head).

Details and suggested fix

generatetoaddress and generatetodescriptor call CreateNewTemplate through generateBlocks; generateblock calls it directly. All retain the default validity check, so a BlockAssembler exception escapes before their later block-generation handling. The mocktime setup in interface_ipc_mining.py specifies a trigger: six blocks at the node's future-time limit put median time past at that limit, making the next template time-too-new until local time advances one second. This is a conditional error-code inconsistency relevant to mining test tooling, not a change in block validity. Move the new translation into a small RPC helper used at all three template-creation call sites, and extend the timestamp test to a generate RPC.


Coverage details