diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -373,17 +373,12 @@ // Get credentials std::string strRPCUserColonPass; + bool failedToGetAuthCookie = false; if (gArgs.GetArg("-rpcpassword", "") == "") { // Try fall back to cookie-based authentication if no password is // provided if (!GetAuthCookie(&strRPCUserColonPass)) { - throw std::runtime_error(strprintf( - _("Could not locate RPC credentials. No authentication cookie " - "could be found, and RPC password is not set. See " - "-rpcpassword and -stdinrpcpass. Configuration file: (%s)"), - GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) - .string() - .c_str())); + failedToGetAuthCookie = true; } } else { strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + @@ -430,13 +425,30 @@ event_base_dispatch(base.get()); if (response.status == 0) { - throw CConnectionFailed(strprintf( - "couldn't connect to server: %s (code %d)\n(make sure server is " - "running and you are connecting to the correct RPC port)", - http_errorstring(response.error), response.error)); + std::string responseErrorMessage; + if (response.error != -1) { + responseErrorMessage = + strprintf(" (error code %d - \"%s\")", response.error, + http_errorstring(response.error)); + } + throw CConnectionFailed( + strprintf("Could not connect to the server %s:%d%s\n\nMake sure " + "the bitcoind server is running and that you are " + "connecting to the correct RPC port.", + host, port, responseErrorMessage)); } else if (response.status == HTTP_UNAUTHORIZED) { - throw std::runtime_error( - "incorrect rpcuser or rpcpassword (authorization failed)"); + if (failedToGetAuthCookie) { + throw std::runtime_error(strprintf( + _("Could not locate RPC credentials. No authentication cookie " + "could be found, and RPC password is not set. See " + "-rpcpassword and -stdinrpcpass. Configuration file: (%s)"), + GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)) + .string() + .c_str())); + } else { + throw std::runtime_error( + "Authorization failed: Incorrect rpcuser or rpcpassword"); + } } else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR) { diff --git a/test/functional/interface_bitcoin_cli.py b/test/functional/interface_bitcoin_cli.py --- a/test/functional/interface_bitcoin_cli.py +++ b/test/functional/interface_bitcoin_cli.py @@ -37,15 +37,23 @@ self.log.info("Test -stdinrpcpass option") assert_equal(0, self.nodes[0].cli( '-rpcuser={}'.format(user), '-stdinrpcpass', input=password).getblockcount()) - assert_raises_process_error(1, "incorrect rpcuser or rpcpassword", self.nodes[0].cli( + assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli( '-rpcuser={}'.format(user), '-stdinrpcpass', input="foo").echo) self.log.info("Test -stdin and -stdinrpcpass") assert_equal(["foo", "bar"], self.nodes[0].cli('-rpcuser={}'.format(user), '-stdin', '-stdinrpcpass', input=password + "\nfoo\nbar").echo()) - assert_raises_process_error(1, "incorrect rpcuser or rpcpassword", self.nodes[0].cli( + assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli( '-rpcuser={}'.format(user), '-stdin', '-stdinrpcpass', input="foo").echo) + self.log.info("Test connecting to a non-existing server") + assert_raises_process_error( + 1, "Could not connect to the server", self.nodes[0].cli('-rpcport=1').echo) + + self.log.info("Test connecting with non-existing RPC cookie file") + assert_raises_process_error(1, "Could not locate RPC credentials", self.nodes[0].cli( + '-rpccookiefile=does-not-exist', '-rpcpassword=').echo) + self.log.info("Make sure that -getinfo with arguments fails") assert_raises_process_error( 1, "-getinfo takes no arguments", self.nodes[0].cli('-getinfo').help)