Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
handling_errors.c
Go to the documentation of this file.
1
11
12#include <banjo/error.h>
13#include <banjo/log.h>
14#include <banjo/main.h>
15
16#include <stdio.h>
17
19// Example: Basic error reporting
21
22// [Return Errors]
23// Functions that can fail take a bj_error** as their last parameter.
24// Use bj_set_error for literal messages.
25void load_config_file(const char* path, bj_error** error) {
26 (void)path;
27 // Simulate a failure
28 int file_exists = 0;
29
30 if (!file_exists) {
31 bj_set_error(error, BJ_ERROR_FILE_NOT_FOUND, "configuration file missing");
32 return;
33 }
34 // ... normal processing ...
35}
36
37// Use bj_set_error_fmt for formatted messages with runtime values.
38void open_network_port(int port, bj_error** error) {
39 // Simulate a failure
40 int port_available = 0;
41
42 if (!port_available) {
44 "port %d is already in use", port);
45 return;
46 }
47 // ... normal processing ...
48}
49// [Return Errors]
50
52// Example: Error propagation with context
54
55void initialize_server(const char* config_path, int port, bj_error** error) {
56 bj_error* local_err = 0;
57
58 // Try to load config
59 load_config_file(config_path, &local_err);
60 if (local_err != 0) {
61 // Add context and propagate - local_err is consumed
62 bj_propagate_prefixed_error(error, local_err,
63 "While initializing server: ");
64 return;
65 }
66
67 // Try to open port
68 open_network_port(port, &local_err);
69 if (local_err != 0) {
70 // Alternative: prefix then propagate separately
71 bj_prefix_error_fmt(&local_err, "Cannot bind to port %d: ", port);
72 bj_propagate_error(error, local_err);
73 return;
74 }
75
76 bj_info("Server initialized successfully");
77}
78
80// Example: Error matching by code and kind
82
84 bj_error* err = 0;
85
86 load_config_file("missing.cfg", &err);
87
88 if (err != 0) {
89 // Match specific error code
91 bj_info("Specific match: file not found");
92 }
93
94 // Match error kind (category) - catches all system errors
96 bj_info("Kind match: this is a system error");
97 }
98
99 // Access error details
100 bj_info("Error code: 0x%08X", bj_error_code(err));
101 bj_info("Error message: %s", bj_error_message(err));
102
103 bj_clear_error(&err);
104 }
105}
106
108// Example: Copying errors
110
112 bj_error* original = 0;
113 bj_error* copy = 0;
114
115 open_network_port(8080, &original);
116
117 if (original != 0) {
118 // Create a copy for logging/reporting while keeping original
119 copy = bj_copy_error(original);
120
121 bj_info("Original: %s", bj_error_message(original));
122 bj_info("Copy: %s", bj_error_message(copy));
123
124 // Both must be freed
125 bj_clear_error(&original);
126 bj_clear_error(&copy);
127 }
128}
129
131// Example: Ignoring errors (zero-cost path)
133
135 // Pass NULL to indicate you don't care about error details.
136 // No allocation occurs - just a pointer check and early return.
137 load_config_file("optional.cfg", 0);
138
139 bj_info("Continued despite potential error (zero cost)");
140}
141
143// Main
145
146// [Using bj_error]
147int main(int argc, char* argv[]) {
148 (void)argc;
149 (void)argv;
150
151 bj_info("=== Basic Error Handling ===");
152
153 // To receive error information, pass a pointer to a NULL bj_error*
154 bj_error* error = 0;
155
156 initialize_server("/etc/myapp.conf", 8080, &error);
157
158 if (error != 0) {
159 // The error message now includes context from the call chain
160 bj_err("Startup failed: %s", bj_error_message(error));
161
162 // Always clear errors when done to free memory
163 bj_clear_error(&error);
164 }
165
166 bj_info("\n=== Error Matching ===");
168
169 bj_info("\n=== Error Copying ===");
171
172 bj_info("\n=== Zero-Cost Path ===");
174
175 return 0;
176}
177// [Using bj_error]
int main(int argc, char *argv[])
Definition audio_pcm.c:177
Recoverable error handling.
struct bj_error bj_error
Definition api.h:333
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:249
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:265
@ BJ_ERROR_SYSTEM
Generic operating system error.
Definition error.h:263
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
void demonstrate_zero_cost(void)
void demonstrate_error_copy(void)
void load_config_file(const char *path, bj_error **error)
void demonstrate_error_matching(void)
void open_network_port(int port, bj_error **error)
void initialize_server(const char *config_path, int port, bj_error **error)
Logging utility functions.
Portable main() replacement with platform-aware entry shim.