Browse Source

lightningd/invoice.c: Improve programmatic error reporting for `delinvoice`.

Changelog-Changed: JSON-RPC: `delinvoice` will now report specific error codes: 905 for failing to find the invoice, 906 for the invoice status not matching the parameter.
bump-pyln-proto
ZmnSCPxj jxPCSnmZ 5 years ago
committed by ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent
commit
e7d89cd7de
  1. 4
      common/jsonrpc_errors.h
  2. 15
      doc/lightning-delinvoice.7
  3. 13
      doc/lightning-delinvoice.7.md
  4. 15
      lightningd/invoice.c

4
common/jsonrpc_errors.h

@ -59,12 +59,14 @@ static const errcode_t CONNECT_ALL_ADDRESSES_FAILED = 401;
/* bitcoin-cli plugin errors */ /* bitcoin-cli plugin errors */
#define BCLI_ERROR 400 #define BCLI_ERROR 400
/* Errors from `invoice` command */ /* Errors from `invoice` or `delinvoice` commands */
static const errcode_t INVOICE_LABEL_ALREADY_EXISTS = 900; static const errcode_t INVOICE_LABEL_ALREADY_EXISTS = 900;
static const errcode_t INVOICE_PREIMAGE_ALREADY_EXISTS = 901; static const errcode_t INVOICE_PREIMAGE_ALREADY_EXISTS = 901;
static const errcode_t INVOICE_HINTS_GAVE_NO_ROUTES = 902; static const errcode_t INVOICE_HINTS_GAVE_NO_ROUTES = 902;
static const errcode_t INVOICE_EXPIRED_DURING_WAIT = 903; static const errcode_t INVOICE_EXPIRED_DURING_WAIT = 903;
static const errcode_t INVOICE_WAIT_TIMED_OUT = 904; static const errcode_t INVOICE_WAIT_TIMED_OUT = 904;
static const errcode_t INVOICE_NOT_FOUND = 905;
static const errcode_t INVOICE_STATUS_UNEXPECTED = 906;
/* Errors from HSM crypto operations. */ /* Errors from HSM crypto operations. */
static const errcode_t HSM_ECDH_FAILED = 800; static const errcode_t HSM_ECDH_FAILED = 800;

15
doc/lightning-delinvoice.7

@ -19,6 +19,21 @@ The caller should be particularly aware of the error case caused by the
On success, an invoice description will be returned as per On success, an invoice description will be returned as per
\fBlightning-listinvoice\fR(7)\. \fBlightning-listinvoice\fR(7)\.
.SH ERRORS
The following errors may be reported:
.RS
.IP \[bu]
-1: Database error\.
.IP \[bu]
905: An invoice with that label does not exist\.
.IP \[bu]
906: The invoice \fIstatus\fR does not match the parameter\.
An error object will be returned as error \fIdata\fR, containing
\fIcurrent_status\fR and \fIexpected_status\fR fields\.
.RE
.SH AUTHOR .SH AUTHOR
Rusty Russell \fI<rusty@rustcorp.com.au\fR> is mainly responsible\. Rusty Russell \fI<rusty@rustcorp.com.au\fR> is mainly responsible\.

13
doc/lightning-delinvoice.7.md

@ -21,6 +21,19 @@ RETURN VALUE
On success, an invoice description will be returned as per On success, an invoice description will be returned as per
lightning-listinvoice(7). lightning-listinvoice(7).
ERRORS
------
The following errors may be reported:
- -1: Database error.
- 905: An invoice with that label does not exist.
- 906: The invoice *status* does not match the parameter.
An error object will be returned as error *data*, containing
*current_status* and *expected_status* fields.
This is most likely due to the *status* of the invoice
changing just before this command is invoked.
AUTHOR AUTHOR
------ ------

15
lightningd/invoice.c

@ -1145,7 +1145,7 @@ static struct command_result *json_delinvoice(struct command *cmd,
return command_param_failed(); return command_param_failed();
if (!wallet_invoice_find_by_label(wallet, &i, label)) { if (!wallet_invoice_find_by_label(wallet, &i, label)) {
return command_fail(cmd, LIGHTNINGD, "Unknown invoice"); return command_fail(cmd, INVOICE_NOT_FOUND, "Unknown invoice");
} }
details = wallet_invoice_details(cmd, cmd->ld->wallet, i); details = wallet_invoice_details(cmd, cmd->ld->wallet, i);
@ -1154,15 +1154,22 @@ static struct command_result *json_delinvoice(struct command *cmd,
* might not make sense if it changed! */ * might not make sense if it changed! */
actual_status = invoice_status_str(details); actual_status = invoice_status_str(details);
if (!streq(actual_status, status)) { if (!streq(actual_status, status)) {
return command_fail(cmd, LIGHTNINGD, struct json_stream *js;
"Invoice status is %s not %s", js = json_stream_fail(cmd, INVOICE_STATUS_UNEXPECTED,
actual_status, status); tal_fmt(tmpctx,
"Invoice status is %s not %s",
actual_status, status));
json_add_string(js, "current_status", actual_status);
json_add_string(js, "expected_status", status);
json_object_end(js);
return command_failed(cmd, js);
} }
if (!wallet_invoice_delete(wallet, i)) { if (!wallet_invoice_delete(wallet, i)) {
log_broken(cmd->ld->log, log_broken(cmd->ld->log,
"Error attempting to remove invoice %"PRIu64, "Error attempting to remove invoice %"PRIu64,
i.id); i.id);
/* FIXME: allocate a generic DATABASE_ERROR code. */
return command_fail(cmd, LIGHTNINGD, "Database error"); return command_fail(cmd, LIGHTNINGD, "Database error");
} }

Loading…
Cancel
Save