The key properties are:
-
You can pre-allocate errors like "out-of-memory".
-
A simple error forwarding pattern based on a macro:
U7_RETURN_IF_ERROR(fn());
Or the same achieved manually:
u7_error error = u7_ok();
if ((error = fn()).error_code) {
return err;
}
return u7_ok();
- Error handling pattern:
u7_error error = u7_ok();
if ((error = fn()).error_code) {
fprintf(stderr, "%" U7_ERROR_FMT "\n", U7_ERROR_FMT_PARAMS(error));
<error-handling>
u7_error_release(error);
}
The library provides a mechanism for initializers that can be registered in any module and triggered from main()
.
Registration:
// any.c
#include <github.com/apronchenkov/u7_init/public/error.h>
#include <github.com/apronchenkov/u7_init/public/init.h>
static u7_error initializer_fn(void) {
// initialization logic
return u7_ok();
}
U7_REGISTER_INITIALIZER((int) precedence, (const char*) name, &initializer_fn)
Triggering:
// main.c
#include <github.com/apronchenkov/u7_init/public/error.h>
#include <github.com/apronchenkov/u7_init/public/init.h>
#include <stdio.h>
int main(int argc, char** argv) {
u7_error error = u7_init();
if (error.error_code) {
fprintf(stderr, "%" U7_ERROR_FMT "\n", U7_ERROR_FMT_PARAMS(error));
u7_error_release(error);
return -1;
}
...
}