commando: correctly reflect error data field.

Some JSON error include "data", and we should reflect that.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-07-16 22:48:27 +09:30
parent 49df89556b
commit b49703e279
2 changed files with 27 additions and 5 deletions

View file

@ -1,5 +1,6 @@
#include "config.h" #include "config.h"
#include <ccan/array_size/array_size.h> #include <ccan/array_size/array_size.h>
#include <ccan/json_out/json_out.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <common/json_param.h> #include <common/json_param.h>
#include <common/json_stream.h> #include <common/json_stream.h>
@ -331,8 +332,10 @@ static struct command_result *handle_reply(struct node_id *peer,
err = json_get_member(replystr, toks, "error"); err = json_get_member(replystr, toks, "error");
if (err) { if (err) {
const jsmntok_t *code = json_get_member(replystr, err, "code"); const jsmntok_t *code = json_get_member(replystr, err, "code");
int ecode;
const jsmntok_t *message = json_get_member(replystr, err, "message"); const jsmntok_t *message = json_get_member(replystr, err, "message");
const jsmntok_t *datatok = json_get_member(replystr, err, "data");
struct json_out *data;
int ecode;
if (!code || !json_to_int(replystr, code, &ecode)) { if (!code || !json_to_int(replystr, code, &ecode)) {
return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL, return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL,
"Error '%.*s' had no valid code", "Error '%.*s' had no valid code",
@ -343,10 +346,17 @@ static struct command_result *handle_reply(struct node_id *peer,
return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL, return command_fail(ocmd->cmd, COMMANDO_ERROR_LOCAL,
"Error had no message"); "Error had no message");
} }
/* FIXME: data! */ if (datatok) {
return command_fail(ocmd->cmd, ecode, "%.*s", data = json_out_new(ocmd->cmd);
message->end - message->start, memcpy(json_out_direct(data, json_tok_full_len(datatok)),
replystr + message->start); json_tok_full(replystr, datatok),
json_tok_full_len(datatok));
} else
data = NULL;
return command_done_err(ocmd->cmd, ecode,
json_strdup(tmpctx, replystr, message),
data);
} }
result = json_get_member(replystr, toks, "result"); result = json_get_member(replystr, toks, "result");

View file

@ -2598,3 +2598,15 @@ def test_commando(node_factory):
'deschashonly': True}}) 'deschashonly': True}})
assert 'bolt11' in ret assert 'bolt11' in ret
# This will fail, will include data.
with pytest.raises(RpcError, match='No connection to first peer found') as exc_info:
l2.rpc.call(method='commando',
payload={'peer_id': l1.info['id'],
'method': 'sendpay',
'params': {'route': [{'amount_msat': 1000,
'id': l1.info['id'],
'delay': 12,
'channel': '1x2x3'}],
'payment_hash': '00' * 32}})
assert exc_info.value.error['data']['erring_index'] == 0