Merge bitcoin/bitcoin#32660: rpc: Use type-safe exception to pass RPC help

fa946520d229ae45b30519bccc9eaa2c47b4a093 refactor: Use structured binding for-loop (MarcoFalke)
eeeec1579ec5a3aa7b10ff62f87d197ae311a666 rpc: Use type-safe exception to pass RPC help (MarcoFalke)

Pull request description:

  The current "catch-all" `catch (const std::exception& e)` in `CRPCTable::help` is problematic, because it could catch exceptions unrelated to passing the help string up.

  Fix this by using a dedicated exception type.

ACKs for top commit:
  l0rinc:
    tested ACK fa946520d229ae45b30519bccc9eaa2c47b4a093 (edited)
  achow101:
    ACK fa946520d229ae45b30519bccc9eaa2c47b4a093
  rkrux:
    re-ACK fa946520d229ae45b30519bccc9eaa2c47b4a093

Tree-SHA512: 23dac6e0fe925561bfbf421e6a7441d546eed8c1492ac41ca4ed7dfcd12f4d2ef39c35f105a0291aac511365d98f08fbdc9a4f0bf627172873b8f23c2be45e76
This commit is contained in:
Ava Chow 2025-07-07 17:47:20 -07:00
commit 21b42f3c55
No known key found for this signature in database
GPG Key ID: 17565732E08E5E41
3 changed files with 11 additions and 11 deletions

View File

@ -1,5 +1,5 @@
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2022 The Bitcoin Core developers
// Copyright (c) 2009-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -21,6 +21,7 @@
#include <util/time.h>
#include <validation.h>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <memory>
@ -79,15 +80,13 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
for (const auto& entry : mapCommands)
vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
sort(vCommands.begin(), vCommands.end());
std::ranges::sort(vCommands);
JSONRPCRequest jreq = helpreq;
jreq.mode = JSONRPCRequest::GET_HELP;
jreq.params = UniValue();
for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
{
const CRPCCommand *pcmd = command.second;
for (const auto& [_, pcmd] : vCommands) {
std::string strMethod = pcmd->name;
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
continue;
@ -97,11 +96,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
UniValue unused_result;
if (setDone.insert(pcmd->unique_id).second)
pcmd->actor(jreq, unused_result, /*last_handler=*/true);
}
catch (const std::exception& e)
{
// Help text is returned in an exception
std::string strHelp = std::string(e.what());
} catch (const HelpResult& e) {
std::string strHelp{e.what()};
if (strCommand == "")
{
if (strHelp.find('\n') != std::string::npos)

View File

@ -645,7 +645,7 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
* the user is asking for help information, and throw help when appropriate.
*/
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
throw std::runtime_error(ToString());
throw HelpResult{ToString()};
}
UniValue arg_mismatch{UniValue::VOBJ};
for (size_t i{0}; i < m_args.size(); ++i) {

View File

@ -67,6 +67,10 @@ class FillableSigningProvider;
class CScript;
struct Sections;
struct HelpResult : std::runtime_error {
explicit HelpResult(const std::string& msg) : std::runtime_error{msg} {}
};
/**
* Gets all existing output types formatted for RPC help sections.
*