|
Banjo API 0.0.1
C99 game development API
|
Macros | |
| #define | bj_error_code_kind(code) |
| #define | bj_error_code_is_user(code) |
Enumerations | |
| enum | bj_error_code { BJ_ERROR_NONE = 0x00000000 , BJ_ERROR = 0x00000001 , BJ_ERROR_UNSUPPORTED = 0x00000101 , BJ_ERROR_NOT_IMPLEMENTED = 0x00000201 , BJ_ERROR_SYSTEM = 0x00000002 , BJ_ERROR_FILE_NOT_FOUND = 0x00000102 , BJ_ERROR_CANNOT_ALLOCATE = 0x00000202 , BJ_ERROR_INITIALIZE = 0x00000302 , BJ_ERROR_DISPOSE = 0x00000402 , BJ_ERROR_IO = 0x00000003 , BJ_ERROR_CANNOT_READ = 0x00000103 , BJ_ERROR_CANNOT_WRITE = 0x00000203 , BJ_ERROR_INVALID_DATA = 0x00000004 , BJ_ERROR_INVALID_FORMAT = 0x00000104 , BJ_ERROR_INCORRECT_VALUE = 0x00000204 , BJ_ERROR_VIDEO = 0x00000005 , BJ_ERROR_AUDIO = 0x00000006 } |
Functions | |
| void | bj_set_error (struct bj_error **error, uint32_t code, const char *message) |
| void | bj_set_error_fmt (struct bj_error **error, uint32_t code, const char *format,...) |
| void | bj_propagate_error (struct bj_error **dest, struct bj_error *src) |
| void | bj_propagate_prefixed_error (struct bj_error **dest, struct bj_error *src, const char *format,...) |
| void | bj_prefix_error (struct bj_error **error, const char *prefix) |
| void | bj_prefix_error_fmt (struct bj_error **error, const char *format,...) |
| struct bj_error * | bj_copy_error (const struct bj_error *error) |
| void | bj_clear_error (struct bj_error **error) |
| bj_bool | bj_error_matches (const struct bj_error *error, uint32_t code) |
| bj_bool | bj_error_matches_kind (const struct bj_error *error, uint32_t kind) |
| uint32_t | bj_error_code (const struct bj_error *error) |
| const char * | bj_error_message (const struct bj_error *error) |
Recoverable error handling facilities.
This API provides a mechanism for communicating recoverable errors from callee to caller, inspired by GLib's GError system.
Functions that may fail take a bj_error** as their last parameter. Pass a pointer to a NULL bj_error* to receive error details:
Use bj_set_error or bj_set_error_fmt from inside a function that may fail:
When calling a fallible function from another fallible function, use bj_propagate_error to forward errors up the call stack:
Use bj_prefix_error to add context to an existing error:
Passing NULL for the error parameter indicates you don't care about error details. In this case, no allocation occurs - just a pointer check.
| #define bj_error_code_is_user | ( | code | ) |
Checks if an error code is user-defined (not from Banjo).
Banjo error codes have their most significant byte set to 0x00. User applications can use non-zero values in the MSB to define their own error codes without conflicting with Banjo.
| code | The error code to check. |
| #define bj_error_code_kind | ( | code | ) |
Extracts the kind (category) from an error code.
The kind is the least significant byte, shared by all errors in the same category. For example:
| code | The error code to extract the kind from. |
| enum bj_error_code |
A numeric representation of an error in Banjo.
Error codes are 32-bit values with a hierarchical structure:
For example, BJ_ERROR_INCORRECT_VALUE is 0x00000204:
Use bj_error_code_kind to extract the kind from any error code. Use bj_error_code_is_user to check if an error is user-defined.
| void bj_clear_error | ( | struct bj_error ** | error | ) |
Frees an error and sets the pointer to NULL.
Safe to call with a NULL pointer or pointer to NULL.
| error | Pointer to error to free. |
Creates a copy of an error.
Returns a newly allocated error with the same code and message. The caller owns the copy and must free it with bj_clear_error.
| error | The error to copy. May be NULL. |
| uint32_t bj_error_code | ( | const struct bj_error * | error | ) |
Gets the error code from an error object.
| error | The error to query. May be NULL. |
Checks if an error matches a specific error code.
Returns true if error is non-NULL and has the specified code. Use this for exact matching of error codes.
| error | The error to check. May be NULL. |
| code | The error code to match against. |
Checks if an error belongs to a specific error kind (category).
Returns true if error is non-NULL and its kind matches the specified kind. Use this to handle categories of errors uniformly.
| error | The error to check. May be NULL. |
| kind | The error kind to match (e.g., BJ_ERROR_IO, BJ_ERROR_SYSTEM). |
| const char * bj_error_message | ( | const struct bj_error * | error | ) |
Gets the error message from an error object.
The returned string is owned by the error object and remains valid until the error is freed or modified.
| error | The error to query. May be NULL. |
| void bj_prefix_error | ( | struct bj_error ** | error, |
| const char * | prefix ) |
Adds a prefix to an existing error's message.
Prepends text to the error message to add context about where/why the error occurred.
If *error is NULL, this function does nothing.
| error | Pointer to the error to modify. |
| prefix | Null-terminated prefix string. |
| void bj_prefix_error_fmt | ( | struct bj_error ** | error, |
| const char * | format, | ||
| ... ) |
Adds a formatted prefix to an existing error's message.
Like bj_prefix_error, but with printf-style formatting.
| error | Pointer to the error to modify. |
| format | Printf-style format string for the prefix. |
| ... | Format arguments. |
Propagates an error to the caller's error location.
Transfers ownership of src to *dest. After this call, src is consumed and must not be used or freed.
If dest is NULL, the error is logged and freed (caller doesn't want it). If *dest is already non-NULL, src is logged and freed (first error wins).
Typical usage:
| dest | Caller's error location. May be NULL. |
| src | Error to propagate. Consumed by this call. |
| void bj_propagate_prefixed_error | ( | struct bj_error ** | dest, |
| struct bj_error * | src, | ||
| const char * | format, | ||
| ... ) |
Propagates an error with an added prefix.
Combines bj_prefix_error_fmt and bj_propagate_error in one call. Adds context to the error message before propagating.
| dest | Caller's error location. May be NULL. |
| src | Error to propagate. Consumed by this call. |
| format | Printf-style prefix format. |
| ... | Format arguments. |
| void bj_set_error | ( | struct bj_error ** | error, |
| uint32_t | code, | ||
| const char * | message ) |
Creates a new error with a literal message.
Allocates and initializes a new error object. Use this when the error message is a compile-time constant or doesn't need formatting.
If *error is already non-NULL, the new error is logged but not stored, preserving the original error (first error wins).
If error is NULL, the error is logged in debug builds but no allocation occurs (zero cost).
| error | Pointer to error location. May be NULL. |
| code | Error code from bj_error_code or user-defined. |
| message | Null-terminated error message. |
| void bj_set_error_fmt | ( | struct bj_error ** | error, |
| uint32_t | code, | ||
| const char * | format, | ||
| ... ) |
Creates a new error with a formatted message.
Like bj_set_error, but accepts printf-style format arguments.
| error | Pointer to error location. May be NULL. |
| code | Error code. |
| format | Printf-style format string. |
| ... | Format arguments. |