-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patherrors.zig
More file actions
71 lines (60 loc) · 2.04 KB
/
Copy patherrors.zig
File metadata and controls
71 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const std = @import("std");
const assert = @import("std").debug.assert;
const c = @import("util.zig").c;
pub const HeaderError = error{
BadIndex,
Missing,
NoHeaders,
NoRequest,
OutOfMemory,
BadArgument,
NotBuiltIn,
UnknownHeaderError,
/// This means there is no `curl_easy_header` method in current libcurl.
NoCurlHeaderSupport,
};
pub fn headerErrorFrom(code: c.CURLHcode) ?HeaderError {
// https://curl.se/libcurl/c/libcurl-errors.html
return switch (code) {
c.CURLHE_OK => null,
c.CURLHE_BADINDEX => error.BadIndex,
c.CURLHE_MISSING => error.Missing,
c.CURLHE_NOHEADERS => error.NoHeaders,
c.CURLHE_NOREQUEST => error.NoRequest,
c.CURLHE_OUT_OF_MEMORY => error.OutOfMemory,
c.CURLHE_BAD_ARGUMENT => error.BadArgument,
c.CURLHE_NOT_BUILT_IN => error.NotBuiltIn,
else => error.UnknownHeaderError,
};
}
/// Information about the success or failure of the curl call.
pub const Diagnostics = struct {
error_code: ?union(enum) {
/// https://curl.se/libcurl/c/libcurl-errors.html
code: c.CURLcode,
/// 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)),
};
}
};
pub fn checkCode(code: c.CURLcode, diagnostics: ?*Diagnostics) !void {
if (code == c.CURLE_OK) {
return;
}
if (diagnostics) |diag| diag.error_code = .{ .code = code };
return error.Curl;
}
pub fn checkMCode(code: c.CURLMcode, diagnostics: ?*Diagnostics) !void {
if (code == c.CURLM_OK) {
return;
}
if (diagnostics) |diag| diag.error_code = .{ .m_code = code };
return error.Curl;
}