Skip to content

Commit ff41a36

Browse files
committed
cli: extract ParseResult() and ParseError()
and make callable higher up with (nRet == 0) check.
1 parent f4185b2 commit ff41a36

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

src/bitcoin-cli.cpp

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,34 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
475475
return response;
476476
}
477477

478+
/** Parse UniValue result to update the message to print to std::cout. */
479+
static void ParseResult(const UniValue& result, std::string& strPrint)
480+
{
481+
if (result.isNull()) return;
482+
strPrint = result.isStr() ? result.get_str() : result.write(2);
483+
}
484+
485+
/** Parse UniValue error to update the message to print to std::cerr and the code to return. */
486+
static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
487+
{
488+
if (error.isObject()) {
489+
const UniValue& err_code = find_value(error, "code");
490+
const UniValue& err_msg = find_value(error, "message");
491+
if (!err_code.isNull()) {
492+
strPrint = "error code: " + err_code.getValStr() + "\n";
493+
}
494+
if (err_msg.isStr()) {
495+
strPrint += ("error message:\n" + err_msg.get_str());
496+
}
497+
if (err_code.isNum() && err_code.get_int() == RPC_WALLET_NOT_SPECIFIED) {
498+
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
499+
}
500+
} else {
501+
strPrint = "error: " + error.write();
502+
}
503+
nRet = abs(error["code"].get_int());
504+
}
505+
478506
/**
479507
* GetWalletBalances calls listwallets; if more than one wallet is loaded, it then
480508
* fetches mine.trusted balances for each loaded wallet and pushes them to `result`.
@@ -575,40 +603,22 @@ static int CommandLineRPC(int argc, char *argv[])
575603
method = args[0];
576604
args.erase(args.begin()); // Remove trailing method name from arguments vector
577605
}
578-
Optional<std::string> wallet_name{};
579-
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
580-
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
581-
582-
// Parse reply
583-
UniValue result = find_value(reply, "result");
584-
const UniValue& error = find_value(reply, "error");
585-
if (!error.isNull()) {
586-
// Error
587-
strPrint = "error: " + error.write();
588-
nRet = abs(error["code"].get_int());
589-
if (error.isObject()) {
590-
const UniValue& errCode = find_value(error, "code");
591-
const UniValue& errMsg = find_value(error, "message");
592-
strPrint = errCode.isNull() ? "" : ("error code: " + errCode.getValStr() + "\n");
593-
594-
if (errMsg.isStr()) {
595-
strPrint += ("error message:\n" + errMsg.get_str());
596-
}
597-
if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) {
598-
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
606+
if (nRet == 0) {
607+
// Perform RPC call
608+
Optional<std::string> wallet_name{};
609+
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
610+
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
611+
612+
// Parse reply
613+
UniValue result = find_value(reply, "result");
614+
const UniValue& error = find_value(reply, "error");
615+
if (error.isNull()) {
616+
if (gArgs.IsArgSet("-getinfo") && !gArgs.IsArgSet("-rpcwallet")) {
617+
GetWalletBalances(result); // fetch multiwallet balances and append to result
599618
}
600-
}
601-
} else {
602-
if (gArgs.IsArgSet("-getinfo") && !gArgs.IsArgSet("-rpcwallet")) {
603-
GetWalletBalances(result); // fetch multiwallet balances and append to result
604-
}
605-
// Result
606-
if (result.isNull()) {
607-
strPrint = "";
608-
} else if (result.isStr()) {
609-
strPrint = result.get_str();
619+
ParseResult(result, strPrint);
610620
} else {
611-
strPrint = result.write(2);
621+
ParseError(error, strPrint, nRet);
612622
}
613623
}
614624
} catch (const std::exception& e) {

0 commit comments

Comments
 (0)