Skip to content

Commit 5c254b1

Browse files
author
Felix "xq" Queißner
committed
Improves error reporting a bit.
1 parent ccaa61b commit 5c254b1

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

justfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ behaviour-tests: \
2424
(behaviour-test "tests/part/mbr/basic-single-part-sized.dis") \
2525
(behaviour-test "tests/fs/fat12.dis") \
2626
(behaviour-test "tests/fs/fat16.dis") \
27-
(behaviour-test "tests/fs/fat32.dis")
27+
(behaviour-test "tests/fs/fat32.dis") \
28+
(behaviour-test "tests/compound/mbr-boot.dis")
2829

2930
behaviour-test script: install
3031
@mkdir -p {{ join(out, parent_directory(script)) }}

src/dim.zig

+30-8
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,21 @@ pub const Context = struct {
228228
pub fn parse_enum(ctx: Context, comptime E: type) Environment.ParseError!E {
229229
if (@typeInfo(E) != .@"enum")
230230
@compileError("get_enum requires an enum type!");
231-
return std.meta.stringToEnum(
231+
const tag_name = try ctx.parse_string();
232+
const converted = std.meta.stringToEnum(
232233
E,
233-
try ctx.parse_string(),
234-
) orelse return error.InvalidEnumTag;
234+
tag_name,
235+
);
236+
if (converted) |ok|
237+
return ok;
238+
std.debug.print("detected invalid enum tag for {s}: \"{}\"\n", .{ @typeName(E), std.zig.fmtEscapes(tag_name) });
239+
std.debug.print("valid options are:\n", .{});
240+
241+
for (std.enums.values(E)) |val| {
242+
std.debug.print("- '{s}'\n", .{@tagName(val)});
243+
}
244+
245+
return error.InvalidEnumTag;
235246
}
236247

237248
pub fn parse_integer(ctx: Context, comptime I: type, base: u8) Environment.ParseError!I {
@@ -355,14 +366,25 @@ const Environment = struct {
355366
fn fetch_file(io: *const Parser.IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath }![]const u8 {
356367
const env: *const Environment = @fieldParentPtr("io", io);
357368

358-
const name: FileName = .{ .root_dir = env.include_base, .rel_path = path };
359-
try name.declare_dependency();
360-
361-
return env.include_base.readFileAlloc(allocator, path, max_script_size) catch |err| switch (err) {
369+
const contents = env.include_base.readFileAlloc(allocator, path, max_script_size) catch |err| switch (err) {
362370
error.OutOfMemory => return error.OutOfMemory,
363-
error.FileNotFound => return error.FileNotFound,
371+
error.FileNotFound => {
372+
const ctx = Context{ .env = @constCast(env) };
373+
var buffer: [std.fs.max_path_bytes]u8 = undefined;
374+
try ctx.report_nonfatal_error("failed to open file: \"{}/{}\"", .{
375+
std.zig.fmtEscapes(env.include_base.realpath(".", &buffer) catch return error.FileNotFound),
376+
std.zig.fmtEscapes(path),
377+
});
378+
return error.FileNotFound;
379+
},
364380
else => return error.IoError,
365381
};
382+
errdefer allocator.free(contents);
383+
384+
const name: FileName = .{ .root_dir = env.include_base, .rel_path = path };
385+
try name.declare_dependency();
386+
387+
return contents;
366388
}
367389

368390
fn resolve_var(io: *const Parser.IO, name: []const u8) error{UnknownVariable}![]const u8 {

tests/compound/mbr-boot.dis

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
mbr-part
2+
bootloader empty
3+
part # partition 1
4+
type fat16_lba
5+
contains vfat fat16
6+
label "BOOT"
7+
endfat
8+
size 10M
9+
endpart
10+
part # partition 2
11+
type fat16_lba
12+
contains vfat fat16
13+
label "OS"
14+
!include "../../data/rootfs.dis"
15+
endfat
16+
endpart
17+
ignore # partition 3
18+
ignore # partition 4

0 commit comments

Comments
 (0)