mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
Use dedicated type for error codes
Before this patch we used `int` for error codes. The problem with `int` is that we try to pass it to/from wire and the size of `int` is not defined by the standard. So a sender with 4-byte `int` would write 4 bytes to the wire and a receiver with 2-byte `int` (for example) would read just 2 bytes from the wire. To resolve this: * Introduce an error code type with a known size: `typedef s32 errcode_t`. * Change all error code macros to constants of type `errcode_t`. Constants also play better with gdb - it would visualize the name of the constant instead of the numeric value. * Change all functions that take error codes to take the new type `errcode_t` instead of `int`. * Introduce towire / fromwire functions to send / receive the newly added type `errcode_t` and use it instead of `towire_int()`. In addition: * Remove the now unneeded `towire_int()`. * Replace a hardcoded error code `-2` with a new constant `INVOICE_EXPIRED_DURING_WAIT` (903). Changelog-Changed: The waitinvoice command would now return error code 903 to designate that the invoice expired during wait, instead of the previous -2
This commit is contained in:
committed by
ZmnSCPxj, ZmnSCPxj jxPCSmnZ
parent
295ca2a436
commit
55173a56b7
@@ -464,7 +464,7 @@ struct command_result *command_failed(struct command *cmd,
|
||||
return command_raw_complete(cmd, result);
|
||||
}
|
||||
|
||||
struct command_result *command_fail(struct command *cmd, int code,
|
||||
struct command_result *command_fail(struct command *cmd, errcode_t code,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
const char *errmsg;
|
||||
@@ -502,7 +502,7 @@ static void json_command_malformed(struct json_connection *jcon,
|
||||
json_add_string(js, "jsonrpc", "2.0");
|
||||
json_add_literal(js, "id", id, strlen(id));
|
||||
json_object_start(js, "error");
|
||||
json_add_member(js, "code", false, "%d", JSONRPC2_INVALID_REQUEST);
|
||||
json_add_member(js, "code", false, "%" PRIerrcode, JSONRPC2_INVALID_REQUEST);
|
||||
json_add_string(js, "message", error);
|
||||
json_object_end(js);
|
||||
json_object_compat_end(js);
|
||||
@@ -553,7 +553,7 @@ struct json_stream *json_stream_success(struct command *cmd)
|
||||
}
|
||||
|
||||
struct json_stream *json_stream_fail_nodata(struct command *cmd,
|
||||
int code,
|
||||
errcode_t code,
|
||||
const char *errmsg)
|
||||
{
|
||||
struct json_stream *js = json_start(cmd);
|
||||
@@ -561,14 +561,14 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd,
|
||||
assert(code);
|
||||
|
||||
json_object_start(js, "error");
|
||||
json_add_member(js, "code", false, "%d", code);
|
||||
json_add_member(js, "code", false, "%" PRIerrcode, code);
|
||||
json_add_string(js, "message", errmsg);
|
||||
|
||||
return js;
|
||||
}
|
||||
|
||||
struct json_stream *json_stream_fail(struct command *cmd,
|
||||
int code,
|
||||
errcode_t code,
|
||||
const char *errmsg)
|
||||
{
|
||||
struct json_stream *r = json_stream_fail_nodata(cmd, code, errmsg);
|
||||
@@ -704,10 +704,11 @@ rpc_command_hook_callback(struct rpc_command_hook_payload *p,
|
||||
|
||||
custom_return = json_get_member(buffer, tok, "error");
|
||||
if (custom_return) {
|
||||
int code;
|
||||
errcode_t code;
|
||||
const char *errmsg;
|
||||
if (!json_to_int(buffer, json_get_member(buffer, custom_return, "code"),
|
||||
&code))
|
||||
if (!json_to_errcode(buffer,
|
||||
json_get_member(buffer, custom_return, "code"),
|
||||
&code))
|
||||
return was_pending(command_fail(p->cmd, JSONRPC2_INVALID_REQUEST,
|
||||
"Bad response to 'rpc_command' hook: "
|
||||
"'error' object does not contain a code."));
|
||||
|
||||
Reference in New Issue
Block a user