wrap errors in real code with quotes
This commit is contained in:
parent
71f94d0600
commit
f30510f122
@ -31,6 +31,9 @@ pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
|
|||||||
|
|
||||||
const Writer = std.io.Writer(*ErrCtx, error{}, write);
|
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 {
|
fn writer(self: *ErrCtx) Writer {
|
||||||
return Writer{ .context = self };
|
return Writer{ .context = self };
|
||||||
}
|
}
|
||||||
@ -51,7 +54,7 @@ pub fn returnf(
|
|||||||
args: anytype,
|
args: anytype,
|
||||||
comptime ret: anytype,
|
comptime ret: anytype,
|
||||||
) @TypeOf(ret) {
|
) @TypeOf(ret) {
|
||||||
self.writer().print(format, args) catch unreachable;
|
self.print(format, args);
|
||||||
return ret;
|
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 {
|
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;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,11 +70,11 @@ fn execute(
|
|||||||
// https://github.com/ziglang/zig/issues/2473
|
// https://github.com/ziglang/zig/issues/2473
|
||||||
var errc = ErrCtx{};
|
var errc = ErrCtx{};
|
||||||
var passwdFile = fs.cwd().openFile(passwdFname, .{ .mode = .read_only }) catch |err|
|
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();
|
defer passwdFile.close();
|
||||||
|
|
||||||
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err|
|
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();
|
defer groupFile.close();
|
||||||
|
|
||||||
var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
|
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..]);
|
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
|
||||||
try testing.expectEqual(@as(u8, 1), exit_code);
|
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" {
|
test "fail" {
|
||||||
|
Loading…
Reference in New Issue
Block a user