Skip to content

Commit de04fde

Browse files
bitcoin-cli: Provide a better error message when bitcoind is not running
Before this patch: ``` $ bitcoin-cli -testnet echo 'hello world' error: Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (/root/.bitcoin/bitcoin.conf) ``` After this patch: ``` $ bitcoin-cli -testnet echo 'hello world' error: Could not connect to the server 127.0.0.1:18332 Make sure the bitcoind server is running and that you are connecting to the correct RPC port. ```
1 parent 29fad97 commit de04fde

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/bitcoin-cli.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,13 +313,11 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
313313

314314
// Get credentials
315315
std::string strRPCUserColonPass;
316+
bool failedToGetAuthCookie = false;
316317
if (gArgs.GetArg("-rpcpassword", "") == "") {
317318
// Try fall back to cookie-based authentication if no password is provided
318319
if (!GetAuthCookie(&strRPCUserColonPass)) {
319-
throw std::runtime_error(strprintf(
320-
_("Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (%s)"),
321-
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str()));
322-
320+
failedToGetAuthCookie = true;
323321
}
324322
} else {
325323
strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");
@@ -358,11 +356,21 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
358356

359357
event_base_dispatch(base.get());
360358

361-
if (response.status == 0)
362-
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));
363-
else if (response.status == HTTP_UNAUTHORIZED)
364-
throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
365-
else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR)
359+
if (response.status == 0) {
360+
std::string responseErrorMessage;
361+
if (response.error != -1) {
362+
responseErrorMessage = strprintf(" (error code %d - \"%s\")", response.error, http_errorstring(response.error));
363+
}
364+
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));
365+
} else if (response.status == HTTP_UNAUTHORIZED) {
366+
if (failedToGetAuthCookie) {
367+
throw std::runtime_error(strprintf(
368+
_("Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (%s)"),
369+
GetConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str()));
370+
} else {
371+
throw std::runtime_error("Authorization failed: Incorrect rpcuser or rpcpassword");
372+
}
373+
} else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR)
366374
throw std::runtime_error(strprintf("server returned HTTP error %d", response.status));
367375
else if (response.body.empty())
368376
throw std::runtime_error("no response from server");

test/functional/interface_bitcoin_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ def run_test(self):
2929

3030
self.log.info("Test -stdinrpcpass option")
3131
assert_equal(0, self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input=password).getblockcount())
32-
assert_raises_process_error(1, "incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input="foo").echo)
32+
assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input="foo").echo)
3333

3434
self.log.info("Test -stdin and -stdinrpcpass")
3535
assert_equal(["foo", "bar"], self.nodes[0].cli('-rpcuser=%s' % user, '-stdin', '-stdinrpcpass', input=password + "\nfoo\nbar").echo())
36-
assert_raises_process_error(1, "incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdin', '-stdinrpcpass', input="foo").echo)
36+
assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdin', '-stdinrpcpass', input="foo").echo)
3737

3838
self.log.info("Make sure that -getinfo with arguments fails")
3939
assert_raises_process_error(1, "-getinfo takes no arguments", self.nodes[0].cli('-getinfo').help)

0 commit comments

Comments
 (0)