Skip to content

Commit 55f490a

Browse files
committed
Merge #12652: bitcoin-cli: Provide a better error message when bitcoind is not running
8b2ef27 tests: Test connecting with non-existing RPC cookie file (practicalswift) a2b2476 tests: Test connecting to a non-existing server (practicalswift) de04fde bitcoin-cli: Provide a better error message when bitcoind is not running (practicalswift) Pull request description: Provide a better `bitcoin-cli` error message when `bitcoind` is not running. Before this patch: ``` $ killall -9 bitcoind $ 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: ``` $ killall -9 bitcoind $ 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. ``` Tree-SHA512: bb16e1a9a1ac110ee202c3cb99b5d7c5c1e5487a17e6cd101e12dc69e9525c14dc71f37b128c26ad615369a57547f15d0f1e29b207c1b2f2ee4b4ba7105f3433
2 parents e476826 + 8b2ef27 commit 55f490a

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ 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)
37+
38+
self.log.info("Test connecting to a non-existing server")
39+
assert_raises_process_error(1, "Could not connect to the server", self.nodes[0].cli('-rpcport=1').echo)
40+
41+
self.log.info("Test connecting with non-existing RPC cookie file")
42+
assert_raises_process_error(1, "Could not locate RPC credentials", self.nodes[0].cli('-rpccookiefile=does-not-exist', '-rpcpassword=').echo)
3743

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

0 commit comments

Comments
 (0)