A reminder on error handling strategies importance
When you have error code model for returning errors, you are going to be fairly limited in how you can report actual issues.
Here is a good example, taken from the ZStd source code:
You can see that the resulting error is the same, but we have three very different errors. Well, to be fair, we have two types of errors.
The total size is wrong and the number of samples is either too big or too small. There is no way to express that to the calling code, which may be far higher in the stack. There is just: “The source size is wrong” error.
There is actually an attempt at proper error reporting. The DISPLAYLEVEL is a way to report more information about the error, but like any C library, we are talking about creating custom error reporting. The DISPLAYLEVEL macro will write to the standard output if a flag is set. That flag is impossible to be set from outside the compilation unit, as far as I can see. So consuming this from managed code means that I have to just guess what these things are.
You can say a lot about the dangers and complexities of exceptions. But having a good way to report complex errors to the caller is very important. Note that in this case, complex means an arbitrary string generated at error time, not a complex object. An error code is great if you need to handle the error. But if you need to show it to the user, log it or handle it after the fact, a good clear error message is the key.
Comments
My guess better error handling was not part of their MVP - because in unmanaged code, allocating a string buffer for an error message also means it needs to be freed (or you would need a static buffer somewhere which is filled with the error data).
Sebastiaan,
Yes, that is likely. They seem to be focused a lot on the CLI capabilities, and they didn't take into account the time to build a proper error handling strategy for calling code.I consider this to be a baseline requirement. See how I solved this in a toy project:
https://ayende.com/blog/185250-C/refactoring-c-code-error-handling-is-hard-error-reporting-is-much-harder
This is similar to the OpenSSL approach. It handles the issue of memory management for error handling quite well
Comment preview