-
Notifications
You must be signed in to change notification settings - Fork 32
feat: Introduce Zig error handle #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Will examples still work without |
Nope, zig enforces that you must handle errors. I added some a note about this in #91. |
|
I see, well, webui and zig-webui However, I will let @jinzhongjia to test and decide. |
|
great work |
|
@jinzhongjia Thats fine. The reason for the two errors are because webui does not return any errors itself it just returns that something went wrong so I cannot confirm as to what caused the error hence the "Generic Error". |
|
I will probably modify it directly on your pr. Different functions have different meanings, and it should return different errors |
|
@jinzhongjia He used a generic error because webui returns only To solve this, I will implement in webui const char* WEBUI_ERRORS_LIST[] = {
"WebUI not initialized",
"Server not found",
"Parameter is missing",
"File not found",
"Web browser not found",
...
}
if ( ! webui_new_window() ) {
int error_number = webui_get_last_error_number();
printf ("Error: %s", WEBUI_ERRORS_LIST[error_number]);
}Or even simpler version: if ( ! webui_new_window() ) {
printf ("Error: %s", webui_get_last_error_message());
} |
|
@jinzhongjia, @GlowingUmbreon, what do you think? |
|
That looks good and will allow for detecting the type of error. Will it return just a string or will it also return a numeric ID of the error? |
|
I think we need both of them, // WebUI Error Handler
pub const WebUIError = error {
GenericError,
};
pub const WebUIErrorInfo = struct {
num: i32,
msg: [:0]const u8,
};
pub fn getLastError() WebUIErrorInfo {
return .{
.num = c.webui_get_last_error_number(),
.msg = c.webui_get_last_error_message(),
};
}
// WebUI APIs
pub fn newWindow() WebUIError!webui {
if (c.webui_new_window()) {
return .{ .window_handle = c.webui_new_window() };
}
return WebUIError.GenericError;
}Usage: const window = webui.newWindow() catch |err| {
const error = webui.getLastError();
std.debug.print("WebUI Error {d}: {s}\n", .{ error.num, error.msg });
return;
};That's all what wrappers have to do, webui will take care of providing error |
|
In this way all Zig examples are not forced to use |
|
Great idea |
|
Initial implementation (webui-dev/webui@2cb977f). @GlowingUmbreon Thank you for the suggestion 👍 |
|
@AlbertShown @hassandraga |
|
I have also made some fixes, but there are some problems with the current binding function, I will make an extra pr fix later |
|
@AlbertShown |
What do you mean?
We can add auto test when we release the last beta before release. |
|
|
|
|
Adds some error handling.
Not all functions have been completed, I have focused on the functions that are easier to handle errors for and added TODO comments in places which require some further thought.
In particular the interfaceGet functions will be be harder to detect errors for as they could either be valid inputs or it could be a error.