-
Notifications
You must be signed in to change notification settings - Fork 39
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
Public C API #558
Comments
Can a roadmap item be added for all-around better error handling? The existing API has zero error handling, and though I'm moving to Fizzy from wasm3 due to some convoluted blocking bugs, wasm3 has much better error handling in its API that I'm really missing. Right now, it's just "welp, it failed" if some of the resulting pointers are NULL - there's nothing to help understand why something failed. |
You are absolutely right. I was integrating fizzy into something else the other day and the lack of error message propagation is annoying. We'll think about a good API for this for the C layer, but if you have some ideas please do not be afraid to share them. |
@axic an extra parameter that accepts a pointer to an error code would be great. Alternatively, changing the return types to be an error code (or success code of course) and having the pointer results be returned via pointer-to-pointer arguments. For example: FizzyModule *fz_mod;
int r = fizzy_parse(
&fz_mod,
mod_bytes.data(),
mod_bytes.size()
);
if (r != 0) {
fprintf(stderr, "error: failed to parse module: %s\n", fizzy_strerr(r));
} else {
assert(fz_mod != NULL);
/* fz_mod is valid */
} |
I think we want to go beyond error codes to have the capability of returning messages. While error codes work well for many cases, they do not for telling about import resolutions problems (i.e. including namespace, module, etc.) There are two problems with returning messages though: a) who manages the memory allocated for them; b) can we avoid allocating memory. In the following option
In the following option we use a global buffer, which can be queried. The downside is that subsequent calls overwrite it and it is not thread safe.
Lastly, we return a message object which has to be explicitly free'd by the caller:
|
@Qix- another question: should we have a public C++ API, would that be preferable? I'm not saying we'll have it anytime soon, but it is only missing due to lack of time. |
Something like this seems ok (just a sketch), but happy to review C API design from other libraries. struct FizzyError { int error_code; const char* message; }
FizzyError err;
fizzy_parse(..., &err);
if (err.error_code != 0)
printf("%s", err.message) && exit(err.error_code);
fizzy_instantiate(..., &err);
if (err.error_code != 0)
printf("%s", err.message) && exit(err.error_code);
fizzy_free_error(&err); So the |
Yes, that matches my first idea. Though I was wondering if the struct should be opaque or not. However, when it is reusable then subsequent users must check if |
No. This can be handled inside Fizzy. Only after the last use the manual free is needed. Alternatively, we can adopt this |
My wording was unclear, by "subsequent users" I meant subsequent calls. So I agree 😉 |
The problem with error strings is two-fold:
This is why many C libraries provide a I would recommend doing it this way. In the event you do decide to go with strings, please under no circumstances perform an allocation. This could cause a cascade of failures that makes debugging (especially in production) very difficult (a memory error would cause a memory-related crash, masking the originating Fizzy-related error - or, many error codes could be generated that are ultimately ignored, but a forgotten Any error-related strings should be strictly statically allocated. |
The text was updated successfully, but these errors were encountered: