-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patherror.zig
44 lines (41 loc) · 1.45 KB
/
error.zig
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
const std = @import("std");
const module = @import("module.zig");
/// A function can take a reference to this to pass extra error information to the caller.
/// A function that does this guarantees the reference will be populated if it returns error.SeeContext.
/// Error implements a format function.
/// The same error instance can be re-used for multiple calls.
///
/// Example usage:
/// ----
/// var zware_error: Error = undefined;
/// foo(&zware_error) catch |err| switch (err) {
/// error.SeeContext => std.log.err("foo failed: {}", .{zware_error}),
/// else => |err| return err,
/// };
/// ---
pub const Error = union(enum) {
missing_import: module.Import,
any: anyerror,
/// Called by a function that wants to both populate this error instance and let the caller
/// know it's been populated by returning error.SeeContext.
pub fn set(self: *Error, e: Error) error{SeeContext} {
self.* = e;
return error.SeeContext;
}
pub fn format(
self: Error,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = fmt;
_ = options;
switch (self) {
.missing_import => |import| try writer.print(
"missing {s} import '{s}' from module '{s}'",
.{ @tagName(import.desc_tag), import.name, import.module },
),
.any => |e| try writer.print("{s}", .{@errorName(e)}),
}
}
};