-
Notifications
You must be signed in to change notification settings - Fork 22
feat(diagnostics): Improve error diagnostics usability #47
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,50 +19,50 @@ pub const Diagnostics = errors.Diagnostics; | |||||||||||
| const Self = @This(); | ||||||||||||
|
|
||||||||||||
| multi: *c.CURLM, | ||||||||||||
| diagnostics: ?*Diagnostics, | ||||||||||||
| diagnostics: Diagnostics, | ||||||||||||
|
|
||||||||||||
| pub fn init(diagnostics: ?*Diagnostics) !Self { | ||||||||||||
| pub fn init() !Self { | ||||||||||||
| const core = c.curl_multi_init(); | ||||||||||||
| if (core == null) { | ||||||||||||
| return error.InitMulti; | ||||||||||||
| } | ||||||||||||
| return .{ | ||||||||||||
| .multi = core.?, | ||||||||||||
| .diagnostics = diagnostics, | ||||||||||||
| .diagnostics = .{}, | ||||||||||||
| }; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| pub fn deinit(self: Self) void { | ||||||||||||
| _ = self; | ||||||||||||
| pub fn deinit(self: *Self) !void { | ||||||||||||
| return checkMCode(c.curl_multi_cleanup(self.multi), &self.diagnostics); | ||||||||||||
|
Comment on lines
+35
to
+36
|
||||||||||||
| pub fn deinit(self: *Self) !void { | |
| return checkMCode(c.curl_multi_cleanup(self.multi), &self.diagnostics); | |
| pub fn deinit(self: *Self) void { | |
| // Always attempt cleanup; record diagnostics but do not propagate an error. | |
| checkMCode(c.curl_multi_cleanup(self.multi), &self.diagnostics) catch {}; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,15 @@ pub const Diagnostics = struct { | |
| /// https://curl.se/libcurl/c/libcurl-errors.html#CURLMcode | ||
| m_code: c.CURLMcode, | ||
| } = null, | ||
|
|
||
| /// Returns a human-readable error message based on the error code. | ||
| pub fn getMessage(self: Diagnostics) ?[]const u8 { | ||
| const error_code = self.error_code orelse return null; | ||
| return switch (error_code) { | ||
| .code => |code| std.mem.span(c.curl_easy_strerror(code)), | ||
| .m_code => |m_code| std.mem.span(c.curl_multi_strerror(m_code)), | ||
| }; | ||
| } | ||
|
Comment on lines
+44
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To fix this without a breaking API change, you can copy the error message from You'll need to add a thread-local buffer at the file scope: threadlocal var easy_error_buffer: [c.CURL_ERROR_SIZE]u8 = undefined;
Comment on lines
+43
to
+50
|
||
| }; | ||
|
|
||
| pub fn checkCode(code: c.CURLcode, diagnostics: ?*Diagnostics) !void { | ||
|
|
@@ -48,9 +57,6 @@ pub fn checkCode(code: c.CURLcode, diagnostics: ?*Diagnostics) !void { | |
|
|
||
| if (diagnostics) |diag| diag.error_code = .{ .code = code }; | ||
|
|
||
| // https://curl.se/libcurl/c/libcurl-errors.html | ||
| std.log.debug("curl err code:{d}, msg:{s}\n", .{ code, c.curl_easy_strerror(code) }); | ||
|
|
||
| return error.Curl; | ||
| } | ||
|
|
||
|
|
@@ -61,11 +67,5 @@ pub fn checkMCode(code: c.CURLMcode, diagnostics: ?*Diagnostics) !void { | |
|
|
||
| if (diagnostics) |diag| diag.error_code = .{ .m_code = code }; | ||
|
|
||
| // https://curl.se/libcurl/c/libcurl-errors.html | ||
| std.log.debug("curlm err code:{d}, msg:{s}\n", .{ | ||
| code, | ||
| c.curl_multi_strerror(code), | ||
| }); | ||
|
|
||
| return error.Curl; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The README snippet uses
curl.Easy.init(.{ ... }), but...is not valid Zig syntax inside a struct literal, so readers copying this example will get a compile error. Consider using a valid minimal init (e.g..{}) or a concrete option like. { .ca_bundle = ca_bundle }to keep the documentation runnable.