-
Notifications
You must be signed in to change notification settings - Fork 902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use dedicated type for error codes #3441
Conversation
a1f5242
to
5859395
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style checker is complaining (make check-includes
):
Include(s) from common/json.h duplicated in common/json.c:
#include <ccan/short_types/short_types.h>
#include <common/errcode.h>
Include(s) from lightningd/jsonrpc.h duplicated in lightningd/jsonrpc.c:
#include <common/errcode.h>
Include(s) from lightningd/notification.h duplicated in lightningd/notification.c:
#include <common/errcode.h>
Include(s) from lightningd/pay.h duplicated in lightningd/pay.c:
#include <common/errcode.h>
Include(s) from plugins/libplugin.h duplicated in plugins/libplugin.c:
#include <common/errcode.h>
Hmm, this is a readability improvement (though I generally stick with #define for constants), but note that the compiler won't see a difference between errcode_t and int, because typedefs are completely fungible. I couldn't understand how we ever got to/fromwire_int until I traced the git history and saw you introduced it yourself in an earlier patch! :) Ack, pending check fixes. |
inb4 |
In some modern ABIs as well, a |
5859395
to
7b29bf1
Compare
The main reason I used constant variables over macros is to ensure that they are of type
I couldn't understand either how did I end up authoring Btw there is also
Thanks! |
ac9bbae
to
c8974c5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit message:
Changelog-Changed: The invoice command would now return error code 903 to designate that the invoice expired during wait, instead of the previous -2
It is the waitinvoice
command which errors if an invoice expired during wait, not invoice
.
@@ -82,8 +82,7 @@ static struct command_result *tell_waiter(struct command *cmd, | |||
json_add_invoice(response, details); | |||
return command_success(cmd, response); | |||
} else { | |||
/* FIXME: -2 should be a constant in jsonrpc_errors.h. */ | |||
response = json_stream_fail(cmd, -2, | |||
response = json_stream_fail(cmd, INVOICE_EXPIRED_DURING_WAIT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in e3879d9.
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
c8974c5
to
e3879d9
Compare
Fixed in e3879d9. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK e3879d9
Before this patch we used
int
for error codes. The problem withint
is that we try to pass it to/from wire and the size ofint
isnot defined by the standard. So a sender with 4-byte
int
would write4 bytes to the wire and a receiver with 2-byte
int
(for example) wouldread just 2 bytes from the wire.
To resolve this:
Introduce an error code type with a known size:
typedef int32_t 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 ofint
.Introduce towire / fromwire functions to send / receive the newly added
type
errcode_t
and use it instead oftowire_int()
.In addition:
Remove the now unneeded
towire_int()
.Replace a hardcoded error code
-2
with a new constantINVOICE_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