Add custom panic handler to printt which file caused a panic

This commit is contained in:
Karl Seguin
2025-10-01 11:24:41 +08:00
parent c335a545a3
commit 69b65dbd41

View File

@@ -29,6 +29,9 @@ const TestHTTPServer = @import("TestHTTPServer.zig");
const WPT_DIR = "tests/wpt"; const WPT_DIR = "tests/wpt";
// use in custom panic handler
var current_test: ?[]const u8 = null;
pub fn main() !void { pub fn main() !void {
var gpa: std.heap.DebugAllocator(.{}) = .init; var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@@ -77,6 +80,9 @@ pub fn main() !void {
while (try it.next()) |test_file| { while (try it.next()) |test_file| {
defer _ = test_arena.reset(.retain_capacity); defer _ = test_arena.reset(.retain_capacity);
defer current_test = null;
current_test = test_file;
var err_out: ?[]const u8 = null; var err_out: ?[]const u8 = null;
const result = run( const result = run(
test_arena.allocator(), test_arena.allocator(),
@@ -448,3 +454,12 @@ fn httpHandler(req: *std.http.Server.Request) !void {
const file_path = try std.fmt.bufPrint(&buf, WPT_DIR ++ "{s}", .{path}); const file_path = try std.fmt.bufPrint(&buf, WPT_DIR ++ "{s}", .{path});
return TestHTTPServer.sendFile(req, file_path); return TestHTTPServer.sendFile(req, file_path);
} }
pub const panic = std.debug.FullPanic(struct {
pub fn panicFn(msg: []const u8, first_trace_addr: ?usize) noreturn {
if (current_test) |ct| {
std.debug.print("===panic running: {s}===\n", .{ct});
}
std.debug.defaultPanic(msg, first_trace_addr);
}
}.panicFn);