Skip to content

Commit

Permalink
jsonrpc: make error codes an enum.
Browse files Browse the repository at this point in the history
This allows GDB to print values, but also allows us to use them in
'case' statements.  This wasn't allowed before because they're not
constant terms.

This also made it clear there's a clash between two error codes,
so move one.

Signed-off-by: Rusty Russell <[email protected]>
Changelog-Changed: JSON-RPC: Error code from bcli plugin changed from 400 to 500.
  • Loading branch information
rustyrussell committed Sep 19, 2022
1 parent 7fa1364 commit 2da5244
Show file tree
Hide file tree
Showing 31 changed files with 167 additions and 167 deletions.
4 changes: 0 additions & 4 deletions common/errcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

#include <ccan/short_types/short_types.h>

typedef s32 errcode_t;

#define PRIerrcode PRId32

// Setup errors
#define EXITCODE_SUBDAEMON_FAIL 10
#define EXITCODE_PIDFILE_LOCK 11
Expand Down
2 changes: 1 addition & 1 deletion common/json_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct command;
struct command_result;

/* Caller supplied this: param assumes it can call it. */
struct command_result *command_fail(struct command *cmd, errcode_t code,
struct command_result *command_fail(struct command *cmd, enum jsonrpc_errcode code,
const char *fmt, ...)
PRINTF_FMT(3, 4) WARN_UNUSED_RESULT RETURNS_NONNULL;

Expand Down
3 changes: 2 additions & 1 deletion common/json_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
return true;
}

bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcode)
bool json_to_jsonrpc_errcode(const char *buffer, const jsmntok_t *tok,
enum jsonrpc_errcode *errcode)
{
s64 tmp;

Expand Down
4 changes: 3 additions & 1 deletion common/json_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <common/errcode.h>
/* Simple helpers are here: this file contains heavier ones */
#include <common/json_parse_simple.h>
#include <common/jsonrpc_errors.h>

struct json_escape;
struct json_stream;
Expand Down Expand Up @@ -49,7 +50,8 @@ bool json_to_millionths(const char *buffer, const jsmntok_t *tok,
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num);

/* Extract an error code from this (may be a string, or a number literal) */
bool json_to_errcode(const char *buffer, const jsmntok_t *tok, errcode_t *errcode);
bool json_to_jsonrpc_errcode(const char *buffer, const jsmntok_t *tok,
enum jsonrpc_errcode *errcode);

/* Split a json token into 2 tokens given a splitting character */
bool split_tok(const char *buffer, const jsmntok_t *tok,
Expand Down
6 changes: 3 additions & 3 deletions common/json_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ void json_add_tok(struct json_stream *result, const char *fieldname,
memcpy(space, json_tok_full(buffer, tok), json_tok_full_len(tok));
}

void json_add_errcode(struct json_stream *result, const char *fieldname,
errcode_t code)
void json_add_jsonrpc_errcode(struct json_stream *result, const char *fieldname,
enum jsonrpc_errcode code)
{
json_add_primitive_fmt(result, fieldname, "%" PRIerrcode, code);
json_add_primitive_fmt(result, fieldname, "%i", code);
}

void json_add_invstring(struct json_stream *result, const char *invstring)
Expand Down
6 changes: 3 additions & 3 deletions common/json_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <common/amount.h>
#include <common/errcode.h>
#include <common/jsonrpc_errors.h>

struct command;
struct io_conn;
Expand Down Expand Up @@ -260,8 +260,8 @@ void json_add_tok(struct json_stream *result, const char *fieldname,
const jsmntok_t *tok, const char *buffer);

/* Add an error code */
void json_add_errcode(struct json_stream *result, const char *fieldname,
errcode_t code);
void json_add_jsonrpc_errcode(struct json_stream *result, const char *fieldname,
enum jsonrpc_errcode code);

/* Add "bolt11" or "bolt12" field, depending on invstring. */
void json_add_invstring(struct json_stream *result, const char *invstring);
Expand Down
196 changes: 99 additions & 97 deletions common/jsonrpc_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,102 +8,104 @@

#include <common/errcode.h>

/* Standard errors defined by JSON-RPC 2.0 standard */
static const errcode_t JSONRPC2_INVALID_REQUEST = -32600;
static const errcode_t JSONRPC2_METHOD_NOT_FOUND = -32601;
static const errcode_t JSONRPC2_INVALID_PARAMS = -32602;

/* Uncategorized error.
* FIXME: This should be replaced in all places
* with a specific error code, and then removed.
*/
static const errcode_t LIGHTNINGD = -1;

/* Developer error in the parameters to param() call */
static const errcode_t PARAM_DEV_ERROR = -2;

/* Plugin returned an error */
static const errcode_t PLUGIN_ERROR = -3;

/* Plugin terminated while handling a request. */
static const errcode_t PLUGIN_TERMINATED = -4;

/* Lightningd is shutting down while handling a request. */
static const errcode_t LIGHTNINGD_SHUTDOWN = -5;

/* Errors from `pay`, `sendpay`, or `waitsendpay` commands */
static const errcode_t PAY_IN_PROGRESS = 200;
static const errcode_t PAY_RHASH_ALREADY_USED = 201;
static const errcode_t PAY_UNPARSEABLE_ONION = 202;
static const errcode_t PAY_DESTINATION_PERM_FAIL = 203;
static const errcode_t PAY_TRY_OTHER_ROUTE = 204;
static const errcode_t PAY_ROUTE_NOT_FOUND = 205;
static const errcode_t PAY_ROUTE_TOO_EXPENSIVE = 206;
static const errcode_t PAY_INVOICE_EXPIRED = 207;
static const errcode_t PAY_NO_SUCH_PAYMENT = 208;
static const errcode_t PAY_UNSPECIFIED_ERROR = 209;
static const errcode_t PAY_STOPPED_RETRYING = 210;
static const errcode_t PAY_STATUS_UNEXPECTED = 211;
static const errcode_t PAY_OFFER_INVALID = 212;

/* `fundchannel` or `withdraw` errors */
static const errcode_t FUND_MAX_EXCEEDED = 300;
static const errcode_t FUND_CANNOT_AFFORD = 301;
static const errcode_t FUND_OUTPUT_IS_DUST = 302;
static const errcode_t FUNDING_BROADCAST_FAIL = 303;
static const errcode_t FUNDING_STILL_SYNCING_BITCOIN = 304;
static const errcode_t FUNDING_PEER_NOT_CONNECTED = 305;
static const errcode_t FUNDING_UNKNOWN_PEER = 306;
static const errcode_t FUNDING_NOTHING_TO_CANCEL = 307;
static const errcode_t FUNDING_CANCEL_NOT_SAFE = 308;
static const errcode_t FUNDING_PSBT_INVALID = 309;
static const errcode_t FUNDING_V2_NOT_SUPPORTED = 310;
static const errcode_t FUNDING_UNKNOWN_CHANNEL = 311;
static const errcode_t FUNDING_STATE_INVALID = 312;

/* `connect` errors */
static const errcode_t CONNECT_NO_KNOWN_ADDRESS = 400;
static const errcode_t CONNECT_ALL_ADDRESSES_FAILED = 401;
static const errcode_t CONNECT_DISCONNECTED_DURING = 402;

/* bitcoin-cli plugin errors */
#define BCLI_ERROR 400

/* Errors from `invoice` or `delinvoice` commands */
static const errcode_t INVOICE_LABEL_ALREADY_EXISTS = 900;
static const errcode_t INVOICE_PREIMAGE_ALREADY_EXISTS = 901;
static const errcode_t INVOICE_HINTS_GAVE_NO_ROUTES = 902;
static const errcode_t INVOICE_EXPIRED_DURING_WAIT = 903;
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;
static const errcode_t INVOICE_OFFER_INACTIVE = 907;
static const errcode_t INVOICE_NO_DESCRIPTION = 908;

/* Errors from HSM crypto operations. */
static const errcode_t HSM_ECDH_FAILED = 800;

/* Errors from `offer` commands */
static const errcode_t OFFER_ALREADY_EXISTS = 1000;
static const errcode_t OFFER_ALREADY_DISABLED = 1001;
static const errcode_t OFFER_EXPIRED = 1002;
static const errcode_t OFFER_ROUTE_NOT_FOUND = 1003;
static const errcode_t OFFER_BAD_INVREQ_REPLY = 1004;
static const errcode_t OFFER_TIMEOUT = 1005;

/* Errors from datastore command */
static const errcode_t DATASTORE_DEL_DOES_NOT_EXIST = 1200;
static const errcode_t DATASTORE_DEL_WRONG_GENERATION = 1201;
static const errcode_t DATASTORE_UPDATE_ALREADY_EXISTS = 1202;
static const errcode_t DATASTORE_UPDATE_DOES_NOT_EXIST = 1203;
static const errcode_t DATASTORE_UPDATE_WRONG_GENERATION = 1204;
static const errcode_t DATASTORE_UPDATE_HAS_CHILDREN = 1205;
static const errcode_t DATASTORE_UPDATE_NO_CHILDREN = 1206;

/* Errors from signmessage command */
static const errcode_t SIGNMESSAGE_PUBKEY_NOT_FOUND = 1301;

/* Errors from wait* commands */
static const errcode_t WAIT_TIMEOUT = 2000;
enum jsonrpc_errcode {
/* Standard errors defined by JSON-RPC 2.0 standard */
JSONRPC2_INVALID_REQUEST = -32600,
JSONRPC2_METHOD_NOT_FOUND = -32601,
JSONRPC2_INVALID_PARAMS = -32602,

/* Uncategorized error.
* FIXME: This should be replaced in all places
* with a specific error code, and then removed.
*/
LIGHTNINGD = -1,

/* Developer error in the parameters to param() call */
PARAM_DEV_ERROR = -2,

/* Plugin returned an error */
PLUGIN_ERROR = -3,

/* Plugin terminated while handling a request. */
PLUGIN_TERMINATED = -4,

/* Lightningd is shutting down while handling a request. */
LIGHTNINGD_SHUTDOWN = -5,

/* Errors from `pay`, `sendpay`, or `waitsendpay` commands */
PAY_IN_PROGRESS = 200,
PAY_RHASH_ALREADY_USED = 201,
PAY_UNPARSEABLE_ONION = 202,
PAY_DESTINATION_PERM_FAIL = 203,
PAY_TRY_OTHER_ROUTE = 204,
PAY_ROUTE_NOT_FOUND = 205,
PAY_ROUTE_TOO_EXPENSIVE = 206,
PAY_INVOICE_EXPIRED = 207,
PAY_NO_SUCH_PAYMENT = 208,
PAY_UNSPECIFIED_ERROR = 209,
PAY_STOPPED_RETRYING = 210,
PAY_STATUS_UNEXPECTED = 211,
PAY_OFFER_INVALID = 212,

/* `fundchannel` or `withdraw` errors */
FUND_MAX_EXCEEDED = 300,
FUND_CANNOT_AFFORD = 301,
FUND_OUTPUT_IS_DUST = 302,
FUNDING_BROADCAST_FAIL = 303,
FUNDING_STILL_SYNCING_BITCOIN = 304,
FUNDING_PEER_NOT_CONNECTED = 305,
FUNDING_UNKNOWN_PEER = 306,
FUNDING_NOTHING_TO_CANCEL = 307,
FUNDING_CANCEL_NOT_SAFE = 308,
FUNDING_PSBT_INVALID = 309,
FUNDING_V2_NOT_SUPPORTED = 310,
FUNDING_UNKNOWN_CHANNEL = 311,
FUNDING_STATE_INVALID = 312,

/* `connect` errors */
CONNECT_NO_KNOWN_ADDRESS = 400,
CONNECT_ALL_ADDRESSES_FAILED = 401,
CONNECT_DISCONNECTED_DURING = 402,

/* bitcoin-cli plugin errors */
BCLI_ERROR = 500,

/* Errors from `invoice` or `delinvoice` commands */
INVOICE_LABEL_ALREADY_EXISTS = 900,
INVOICE_PREIMAGE_ALREADY_EXISTS = 901,
INVOICE_HINTS_GAVE_NO_ROUTES = 902,
INVOICE_EXPIRED_DURING_WAIT = 903,
INVOICE_WAIT_TIMED_OUT = 904,
INVOICE_NOT_FOUND = 905,
INVOICE_STATUS_UNEXPECTED = 906,
INVOICE_OFFER_INACTIVE = 907,
INVOICE_NO_DESCRIPTION = 908,

/* Errors from HSM crypto operations. */
HSM_ECDH_FAILED = 800,

/* Errors from `offer` commands */
OFFER_ALREADY_EXISTS = 1000,
OFFER_ALREADY_DISABLED = 1001,
OFFER_EXPIRED = 1002,
OFFER_ROUTE_NOT_FOUND = 1003,
OFFER_BAD_INVREQ_REPLY = 1004,
OFFER_TIMEOUT = 1005,

/* Errors from datastore command */
DATASTORE_DEL_DOES_NOT_EXIST = 1200,
DATASTORE_DEL_WRONG_GENERATION = 1201,
DATASTORE_UPDATE_ALREADY_EXISTS = 1202,
DATASTORE_UPDATE_DOES_NOT_EXIST = 1203,
DATASTORE_UPDATE_WRONG_GENERATION = 1204,
DATASTORE_UPDATE_HAS_CHILDREN = 1205,
DATASTORE_UPDATE_NO_CHILDREN = 1206,

/* Errors from signmessage command */
SIGNMESSAGE_PUBKEY_NOT_FOUND = 1301,

/* Errors from wait* commands */
WAIT_TIMEOUT = 2000,
};

#endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */
2 changes: 1 addition & 1 deletion common/test/run-json_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, errcode_t code UNNEEDED,
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "command_fail called!\n"); abort(); }
Expand Down
2 changes: 1 addition & 1 deletion common/test/run-param.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct command_result {
static struct command_result cmd_failed;

struct command_result *command_fail(struct command *cmd,
errcode_t code, const char *fmt, ...)
enum jsonrpc_errcode code, const char *fmt, ...)
{
failed = true;
va_list ap;
Expand Down
4 changes: 2 additions & 2 deletions connectd/connectd.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,14 +614,14 @@ struct io_plan *connection_out(struct io_conn *conn, struct connecting *connect)
static void connect_failed(struct daemon *daemon,
const struct node_id *id,
const struct wireaddr_internal *addrhint,
errcode_t errcode,
enum jsonrpc_errcode errcode,
const char *errfmt, ...)
PRINTF_FMT(5,6);

static void connect_failed(struct daemon *daemon,
const struct node_id *id,
const struct wireaddr_internal *addrhint,
errcode_t errcode,
enum jsonrpc_errcode errcode,
const char *errfmt, ...)
{
u8 *msg;
Expand Down
2 changes: 1 addition & 1 deletion connectd/connectd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ msgdata,connectd_connect_to_peer,addrhint,?wireaddr_internal,
# Connectd->master: connect failed.
msgtype,connectd_connect_failed,2020
msgdata,connectd_connect_failed,id,node_id,
msgdata,connectd_connect_failed,failcode,errcode_t,
msgdata,connectd_connect_failed,failcode,enum jsonrpc_errcode,
msgdata,connectd_connect_failed,failreason,wirestring,
msgdata,connectd_connect_failed,addrhint,?wireaddr_internal,

Expand Down
4 changes: 2 additions & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void try_reconnect(const tal_t *ctx,
/* We were trying to connect, but they disconnected. */
static void connect_failed(struct lightningd *ld,
const struct node_id *id,
errcode_t errcode,
enum jsonrpc_errcode errcode,
const char *errmsg,
const struct wireaddr_internal *addrhint)
{
Expand Down Expand Up @@ -390,7 +390,7 @@ void connect_failed_disconnect(struct lightningd *ld,
static void handle_connect_failed(struct lightningd *ld, const u8 *msg)
{
struct node_id id;
errcode_t errcode;
enum jsonrpc_errcode errcode;
char *errmsg;
struct wireaddr_internal *addrhint;

Expand Down
Loading

0 comments on commit 2da5244

Please sign in to comment.