diff --git a/src/pivx-cli.cpp b/src/pivx-cli.cpp index 7b2f0d117af05..ce15f3a8629db 100644 --- a/src/pivx-cli.cpp +++ b/src/pivx-cli.cpp @@ -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=\" option to pivx-cli command line."; + } + } } else { // Result if (result.isNull()) diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h index 84cedade73471..11c6c411aae45 100644 --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -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); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e0862bff5b698..b1a2ecf7f9ac1 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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; } @@ -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/ uri-path)."); } void EnsureWalletIsUnlocked(CWallet* const pwallet, bool fAllowAnonOnly) diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index 39c37cf18ba1b..317789f5cc1bf 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -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()