Skip to content

Commit

Permalink
Fix misleading "Method not found" multiwallet errors
Browse files Browse the repository at this point in the history
Raise RPC_WALLET_NOT_SPECIFIED instead of RPC_METHOD_NOT_FOUND when a
required
wallet filename was not specified in an RPC call.

Also raise more specific RPC_WALLET_NOT_FOUND error instead of
RPC_INVALID_PARAMETER in case an invalid wallet was specified, for
consistency.
  • Loading branch information
ryanofsky authored and random-zebra committed Jun 2, 2021
1 parent ce35e1e commit ee52c2e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/pivx-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,18 @@ int CommandLineRPC(int argc, char* argv[])
throw CConnectionFailed("server in warmup");
strPrint = "error: " + error.write();
nRet = abs(code);
if (error.isObject()) {
UniValue errCode = find_value(error, "code");
UniValue errMsg = find_value(error, "message");
strPrint = errCode.isNull() ? "" : "error code: "+errCode.getValStr()+"\n";

if (errMsg.isStr())
strPrint += "error message:\n"+errMsg.get_str();

if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) {
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to pivx-cli command line.";
}
}
} else {
// Result
if (result.isNull())
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ enum RPCErrorCode {
RPC_WALLET_WRONG_ENC_STATE = -15, //! Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
RPC_WALLET_ENCRYPTION_FAILED = -16, //! Failed to encrypt the wallet
RPC_WALLET_ALREADY_UNLOCKED = -17, //! Wallet is already unlocked
RPC_WALLET_NOT_FOUND = -18, //!< Invalid wallet specified
RPC_WALLET_NOT_SPECIFIED = -19, //!< No wallet specified (error when there are multiple wallets loaded)
};

UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);
Expand Down
15 changes: 8 additions & 7 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CWallet* GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
return pwallet;
}
}
throw JSONRPCError(RPC_INVALID_PARAMETER, "Requested wallet does not exist or is not loaded");
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
}
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
}
Expand All @@ -58,13 +58,14 @@ std::string HelpRequiringPassphrase(CWallet* const pwallet)

bool EnsureWalletIsAvailable(CWallet* const pwallet, bool avoidException)
{
if (!pwallet) {
if (!avoidException)
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (wallet disabled)");
else
return false;
if (pwallet) return true;
if (avoidException) return false;
if (::vpwallets.empty()) {
// Wallet RPC methods are disabled if no wallets are loaded.
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
}
return true;
throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
"Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
}

void EnsureWalletIsUnlocked(CWallet* const pwallet, bool fAllowAnonOnly)
Expand Down
9 changes: 2 additions & 7 deletions test/functional/wallet_multiwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,10 @@ def run_test(self):
w1.generate(1)

# accessing invalid wallet fails
assert_raises_rpc_error(-8, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)

# accessing wallet RPC without using wallet endpoint fails
assert_raises_rpc_error(-32601, "Method not found", self.nodes[0].getwalletinfo)
# !TODO: backport bitcoin#11970
#assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)

# !TODO: backport bitcoin#11473
# to un-comment wallet-name checks
assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)

# check w1 wallet balance
w1_info = w1.getwalletinfo()
Expand Down

0 comments on commit ee52c2e

Please sign in to comment.