1
Fork 0

wip handling errors in top level

main
Motiejus Jakštys 2022-06-22 16:23:02 +03:00
parent 62b643dbc2
commit 6aa4b01692
2 changed files with 27 additions and 15 deletions

View File

@ -22,8 +22,8 @@ pub fn write(self: *ErrCtx, bytes: []const u8) error{}!usize {
self.overflow = bytes.len > can_add; self.overflow = bytes.len > can_add;
self.buf.appendSliceAssumeCapacity(bytes[0..math.min(bytes.len, can_add)]); self.buf.appendSliceAssumeCapacity(bytes[0..math.min(bytes.len, can_add)]);
// not adding the final zero is ok, because it needs // not adding the final zero is ok, because it will
// to be ignored in the iterator anyway. // be ignored in the iterator anyway.
_ = self.buf.append(0) catch null; _ = self.buf.append(0) catch null;
return bytes.len; return bytes.len;

View File

@ -40,25 +40,25 @@ fn execute(
allocator: Allocator, allocator: Allocator,
stderr: anytype, stderr: anytype,
argv: []const [*:0]const u8, argv: []const [*:0]const u8,
) !u8 { ) u8 {
const result = flags.parse(argv, &[_]flags.Flag{ const result = flags.parse(argv, &[_]flags.Flag{
.{ .name = "-h", .kind = .boolean }, .{ .name = "-h", .kind = .boolean },
.{ .name = "--passwd", .kind = .arg }, .{ .name = "--passwd", .kind = .arg },
.{ .name = "--group", .kind = .arg }, .{ .name = "--group", .kind = .arg },
.{ .name = "--output", .kind = .arg }, .{ .name = "--output", .kind = .arg },
}) catch { }) catch {
try stderr.writeAll(usage); stderr.writeAll(usage) catch {};
return 1; return 1;
}; };
if (result.boolFlag("-h")) { if (result.boolFlag("-h")) {
try io.getStdOut().writeAll(usage); io.getStdOut().writeAll(usage) catch return 1;
return 0; return 0;
} }
if (result.args.len != 0) { if (result.args.len != 0) {
try stderr.print("ERROR: unknown option '{s}'\n", .{result.args[0]}); stderr.print("ERROR: unknown option '{s}'\n", .{result.args[0]}) catch {};
try stderr.writeAll(usage); stderr.writeAll(usage) catch {};
return 1; return 1;
} }
@ -66,10 +66,20 @@ fn execute(
const groupFname = result.argFlag("--group") orelse "./group"; const groupFname = result.argFlag("--group") orelse "./group";
const outFile = result.argFlag("--output") orelse "./db.turbo"; const outFile = result.argFlag("--output") orelse "./db.turbo";
//std.debug.print("passwd file name: {s}\n", .{passwdFname}); // to catch a specific file.OpenError, wait for
var passwdFile = try fs.cwd().openFile(passwdFname, .{ .mode = .read_only }); // https://github.com/ziglang/zig/issues/2473
var passwdFile = fs.cwd().openFile(
passwdFname,
.{ .mode = .read_only },
) catch |err| {
stderr.print("Error opening {s}: {s}\n", .{ passwdFname, @errorName(err) }) catch {};
return 1;
};
defer passwdFile.close(); defer passwdFile.close();
var groupFile = try fs.cwd().openFile(groupFname, .{ .mode = .read_only }); var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err| {
stderr.print("Error opening {s}: {s}\n", .{ groupFname, @errorName(err) }) catch {};
return 1;
};
defer groupFile.close(); defer groupFile.close();
var err_ctx = ErrCtx{}; var err_ctx = ErrCtx{};
@ -79,9 +89,11 @@ fn execute(
passwdFile.reader(), passwdFile.reader(),
); );
errdefer { errdefer {
var it = err_ctx.iterator(); stderr.print("ERROR", .{}) catch {};
var it = err_ctx.rev();
while (it.next()) |err| while (it.next()) |err|
std.debug.print("ERROR: {s}\n", .{err}); stderr.print(": {s}", .{err}) catch {};
stderr.print("\n", .{}) catch {};
} }
defer for (users) |*user| user.deinit(allocator); defer for (users) |*user| user.deinit(allocator);
defer allocator.free(users); defer allocator.free(users);
@ -118,7 +130,7 @@ test "invalid argument" {
var stderr = ArrayList(u8).init(allocator); var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit(); defer stderr.deinit();
const exit_code = try execute(allocator, stderr.writer(), args[0..]); const exit_code = execute(allocator, stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code); try testing.expectEqual(@as(u8, 1), exit_code);
try testing.expect(mem.startsWith( try testing.expect(mem.startsWith(
u8, u8,
@ -128,8 +140,6 @@ test "invalid argument" {
} }
test "smoke test" { test "smoke test" {
if (true) return error.SkipZigTest;
const allocator = testing.allocator; const allocator = testing.allocator;
var stderr = ArrayList(u8).init(allocator); var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit(); defer stderr.deinit();
@ -181,6 +191,8 @@ test "smoke test" {
"--group", groupPath, "--group", groupPath,
"--output", outPath, "--output", outPath,
}; };
if (true) return error.SkipZigTest;
const exit_code = try execute(allocator, stderr.writer(), args); const exit_code = try execute(allocator, stderr.writer(), args);
try testing.expectEqual(@as(u8, 0), exit_code); try testing.expectEqual(@as(u8, 0), exit_code);