Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
handling_errors.c

How to handle recoverable errors using bj_error.

How to handle recoverable errors using bj_error.

#include <banjo/error.h>
#include <banjo/main.h>
#include <banjo/log.h>
#define CODE 101
void function_returning_error(bj_error** error) {
bj_set_error(error, CODE, "An error occured");
}
void function_calling_failing_function(bj_error** error) {
bj_error* sub_err = 0;
function_returning_error(&sub_err);
if(sub_err != 0) {
bj_forward_error(sub_err, error);
return;
}
bj_info("This should not be printed\n");
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
// If you are not interested by an error, pass 0 to the bj_error* pointer:
function_returning_error(0);
bj_error* error = 0;
function_returning_error(&error);
if(error != 0) {
bj_info("There was an error");
}
if (bj_error_check(error, CODE)) {
bj_info("Error domain and code match");
}
bj_clear_error(&error);
function_calling_failing_function(&error);
if (bj_error_check(error, CODE)) {
bj_info("Error from nested function");
}
return 0;
}
Recoverable error handling.
void bj_set_error(bj_error **p_error, uint32_t code, const char *message)
Fills in a bj_error object with given code and message.
void bj_clear_error(bj_error **p_error)
Clears the given error location.
bj_bool bj_forward_error(bj_error *p_source, bj_error **p_destination)
Forward an error into another error location.
bj_bool bj_error_check(const bj_error *p_error, uint32_t code)
Checks if the given error matches the error code.
Error structure.
Definition error.h:131
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:103
Logging utility functions.