Merge bitcoin/bitcoin#32660: rpc: Use type-safe exception to pass RPC help
fa946520d2refactor: Use structured binding for-loop (MarcoFalke)eeeec1579erpc: 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 ACKfa946520d2(edited) achow101: ACKfa946520d2rkrux: re-ACKfa946520d2Tree-SHA512: 23dac6e0fe925561bfbf421e6a7441d546eed8c1492ac41ca4ed7dfcd12f4d2ef39c35f105a0291aac511365d98f08fbdc9a4f0bf627172873b8f23c2be45e76
This commit is contained in:
3 files changed
+11
-11
No files matched your search
+6
-10
@@ -1,5 +1,5 @@
|
|||||||
// Copyright (c) 2010 Satoshi Nakamoto
|
// 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
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -79,15 +80,13 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
|
|||||||
|
|
||||||
for (const auto& entry : mapCommands)
|
for (const auto& entry : mapCommands)
|
||||||
vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
|
vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
|
||||||
sort(vCommands.begin(), vCommands.end());
|
std::ranges::sort(vCommands);
|
||||||
|
|
||||||
JSONRPCRequest jreq = helpreq;
|
JSONRPCRequest jreq = helpreq;
|
||||||
jreq.mode = JSONRPCRequest::GET_HELP;
|
jreq.mode = JSONRPCRequest::GET_HELP;
|
||||||
jreq.params = UniValue();
|
jreq.params = UniValue();
|
||||||
|
|
||||||
for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
|
for (const auto& [_, pcmd] : vCommands) {
|
||||||
{
|
|
||||||
const CRPCCommand *pcmd = command.second;
|
|
||||||
std::string strMethod = pcmd->name;
|
std::string strMethod = pcmd->name;
|
||||||
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
|
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
|
||||||
continue;
|
continue;
|
||||||
@@ -97,11 +96,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
|
|||||||
UniValue unused_result;
|
UniValue unused_result;
|
||||||
if (setDone.insert(pcmd->unique_id).second)
|
if (setDone.insert(pcmd->unique_id).second)
|
||||||
pcmd->actor(jreq, unused_result, /*last_handler=*/true);
|
pcmd->actor(jreq, unused_result, /*last_handler=*/true);
|
||||||
}
|
} catch (const HelpResult& e) {
|
||||||
catch (const std::exception& e)
|
std::string strHelp{e.what()};
|
||||||
{
|
|
||||||
// Help text is returned in an exception
|
|
||||||
std::string strHelp = std::string(e.what());
|
|
||||||
if (strCommand == "")
|
if (strCommand == "")
|
||||||
{
|
{
|
||||||
if (strHelp.find('\n') != std::string::npos)
|
if (strHelp.find('\n') != std::string::npos)
|
||||||
|
|||||||
+1
-1
@@ -645,7 +645,7 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
|
|||||||
* the user is asking for help information, and throw help when appropriate.
|
* the user is asking for help information, and throw help when appropriate.
|
||||||
*/
|
*/
|
||||||
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
|
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
|
||||||
throw std::runtime_error(ToString());
|
throw HelpResult{ToString()};
|
||||||
}
|
}
|
||||||
UniValue arg_mismatch{UniValue::VOBJ};
|
UniValue arg_mismatch{UniValue::VOBJ};
|
||||||
for (size_t i{0}; i < m_args.size(); ++i) {
|
for (size_t i{0}; i < m_args.size(); ++i) {
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ class FillableSigningProvider;
|
|||||||
class CScript;
|
class CScript;
|
||||||
struct Sections;
|
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.
|
* Gets all existing output types formatted for RPC help sections.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in new issue
Block a user