Recoverable error handling with Banjo's error system.
Recoverable error handling with Banjo's error system.Banjo uses an out-parameter pattern for error handling inspired by GLib. Functions that can fail take a bj_error** parameter to return error information. This allows checking error codes, reading messages, and propagating errors up the call stack without exceptions.
#include <stdio.h>
(void)path;
int file_exists = 0;
if (!file_exists) {
return;
}
}
int port_available = 0;
if (!port_available) {
"port %d is already in use", port);
return;
}
}
if (local_err != 0) {
"While initializing server: ");
return;
}
if (local_err != 0) {
return;
}
bj_info(
"Server initialized successfully");
}
if (err != 0) {
bj_info(
"Specific match: file not found");
}
bj_info(
"Kind match: this is a system error");
}
}
}
if (original != 0) {
}
}
bj_info(
"Continued despite potential error (zero cost)");
}
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
bj_info(
"=== Basic Error Handling ===");
if (error != 0) {
}
bj_info(
"\n=== Error Matching ===");
bj_info(
"\n=== Error Copying ===");
bj_info(
"\n=== Zero-Cost Path ===");
return 0;
}
Recoverable error handling.
struct bj_error bj_error
Definition api.h:275
void bj_propagate_prefixed_error(struct bj_error **dest, struct bj_error *src, const char *format,...)
Propagates an error with an added prefix.
const char * bj_error_message(const struct bj_error *error)
Gets the error message from an error object.
bj_bool bj_error_matches_kind(const struct bj_error *error, uint32_t kind)
Checks if an error belongs to a specific error kind (category).
struct bj_error * bj_copy_error(const struct bj_error *error)
Creates a copy of an error.
void bj_clear_error(struct bj_error **error)
Frees an error and sets the pointer to NULL.
void bj_propagate_error(struct bj_error **dest, struct bj_error *src)
Propagates an error to the caller's error location.
bj_error_code
A numeric representation of an error in Banjo.
Definition error.h:89
void bj_set_error(struct bj_error **error, uint32_t code, const char *message)
Creates a new error with a literal message.
void bj_prefix_error_fmt(struct bj_error **error, const char *format,...)
Adds a formatted prefix to an existing error's message.
bj_bool bj_error_matches(const struct bj_error *error, uint32_t code)
Checks if an error matches a specific error code.
void bj_set_error_fmt(struct bj_error **error, uint32_t code, const char *format,...)
Creates a new error with a formatted message.
@ BJ_ERROR_FILE_NOT_FOUND
Requested file was not found.
Definition error.h:105
@ BJ_ERROR_SYSTEM
Generic operating system error.
Definition error.h:103
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:105
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:133
void demonstrate_zero_cost(void)
Definition handling_errors.c:132
void demonstrate_error_copy(void)
Definition handling_errors.c:109
void load_config_file(const char *path, bj_error **error)
Definition handling_errors.c:23
void demonstrate_error_matching(void)
Definition handling_errors.c:81
void open_network_port(int port, bj_error **error)
Definition handling_errors.c:36
void initialize_server(const char *config_path, int port, bj_error **error)
Definition handling_errors.c:53
Logging utility functions.
Portable main substitution and application callback facilities.