Skip to content

Commit 0a9bcfb

Browse files
reckless: catch and raise usage errors
While the reckless utility provided usage hints, the rpc plugin would simply exit with an unhelpful: { "code": -3, "message": "the reckless process has crashed" } This captures the usage hint from the utility and reports it from the plugin as well. Changelog-Fixed: reckless-rpc plugin now raises incorrect usage from the reckless utility.
1 parent b53e73f commit 0a9bcfb

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

plugins/recklessrpc.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,23 @@ static struct command_result *reckless_fail(struct reckless *reckless,
200200
return command_finished(reckless->cmd, resp);
201201
}
202202

203+
/* Regurgitates the syntax error reported by the utility */
204+
static struct command_result *fail_bad_usage(struct reckless *reckless)
205+
{
206+
char **lines;
207+
lines = tal_strsplit(reckless, reckless->stderrbuf, "\n", STR_EMPTY_OK);
208+
if (lines != NULL)
209+
{
210+
/* The last line of reckless output contains the usage error.
211+
* Capture it for the user. */
212+
int i = 0;
213+
while (lines[i + 1] != NULL)
214+
i++;
215+
return reckless_fail(reckless, lines[i]);
216+
}
217+
return reckless_fail(reckless, "the reckless process has crashed");
218+
}
219+
203220
static void reckless_conn_finish(struct io_conn *conn,
204221
struct reckless *reckless)
205222
{
@@ -239,14 +256,18 @@ static void reckless_conn_finish(struct io_conn *conn,
239256
"Reckless process has crashed (%i).",
240257
WEXITSTATUS(status));
241258
char * err;
242-
if (reckless->process_failed)
243-
err = reckless->process_failed;
244-
else
245-
err = tal_strdup(tmpctx, "the reckless process "
246-
"has crashed");
247-
reckless_fail(reckless, err);
248-
plugin_log(plugin, LOG_UNUSUAL,
249-
"The reckless subprocess has failed.");
259+
if (WEXITSTATUS(status) == 2)
260+
fail_bad_usage(reckless);
261+
else {
262+
if (reckless->process_failed)
263+
err = reckless->process_failed;
264+
else
265+
err = tal_strdup(tmpctx, "the reckless process "
266+
"has crashed");
267+
reckless_fail(reckless, err);
268+
plugin_log(plugin, LOG_UNUSUAL,
269+
"The reckless subprocess has failed.");
270+
}
250271
}
251272
}
252273
tal_free(reckless);

tests/test_reckless.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
import unittest
88
from fixtures import * # noqa: F401,F403
9+
from pyln.client import lightning
910
from pyln.testing.utils import VALGRIND
1011
import pytest
1112

@@ -481,3 +482,21 @@ def test_reckless_notifications(node_factory):
481482
listconfig_log.pop(1)
482483
for log in listconfig_log:
483484
assert node.daemon.is_in_log(f"reckless_log: {{'reckless_log': {{'log': '{log}'", start=0)
485+
486+
487+
def test_reckless_usage(node_factory):
488+
"""The reckless rpc response is more useful if it can pass back incorrect
489+
usage errors."""
490+
node = node_factory.get_node(options={}, may_fail=True, start=False)
491+
node.start(stderr_redir=True)
492+
r = reckless(['searhc', 'testplugpass'],
493+
dir=node.lightning_dir)
494+
# The reckless utility should fail and argparse should provide a usage hint
495+
# as the line of output.
496+
assert r.returncode == 2
497+
assert "reckless: error: argument cmd1: invalid choice: 'searhc' (choose from " in r.stderr[-1]
498+
499+
# The rpc plugin should capture and raise this usage error
500+
with pytest.raises(lightning.RpcError,
501+
match="reckless: error: argument cmd1: invalid choice: 'saerch'"):
502+
node.rpc.reckless('saerch', 'testplugpass')

0 commit comments

Comments
 (0)