1
Fork 0

wrap errors in real code with quotes

main
Motiejus Jakštys 2022-06-28 16:44:44 +03:00
parent 71f94d0600
commit f30510f122
2 changed files with 8 additions and 5 deletions

View File

@ -31,6 +31,9 @@ pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
const Writer = std.io.Writer(*ErrCtx, error{}, write);
// writer is private, because fmt.print(...) can do multiple write(...) calls
// for a single fmt.print(...). It is too easy to mis-use; therefore use the
// wrappers below.
fn writer(self: *ErrCtx) Writer {
return Writer{ .context = self };
}
@ -51,7 +54,7 @@ pub fn returnf(
args: anytype,
comptime ret: anytype,
) @TypeOf(ret) {
self.writer().print(format, args) catch unreachable;
self.print(format, args);
return ret;
}
@ -61,7 +64,7 @@ pub fn wrap(self: *ErrCtx, comptime msg: []const u8) *ErrCtx {
}
pub fn wrapf(self: *ErrCtx, comptime format: []const u8, args: anytype) *ErrCtx {
self.writer().print(format, args) catch unreachable;
self.print(format, args);
return self;
}

View File

@ -70,11 +70,11 @@ fn execute(
// https://github.com/ziglang/zig/issues/2473
var errc = ErrCtx{};
var passwdFile = fs.cwd().openFile(passwdFname, .{ .mode = .read_only }) catch |err|
return fail(errc.wrapf("open {s}", .{passwdFname}), stderr, err);
return fail(errc.wrapf("open '{s}'", .{passwdFname}), stderr, err);
defer passwdFile.close();
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err|
return fail(errc.wrapf("open {s}", .{groupFname}), stderr, err);
return fail(errc.wrapf("open '{s}'", .{groupFname}), stderr, err);
defer groupFile.close();
var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
@ -151,7 +151,7 @@ test "trivial error: missing passwd file" {
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code);
try testing.expectEqualStrings(stderr.items, "ERROR FileNotFound: ./passwd: open \n");
try testing.expectEqualStrings(stderr.items, "ERROR FileNotFound: open './passwd'\n");
}
test "fail" {