Skip to content

Commit 7091b37

Browse files
committed
Make stacktraces available in debug via page.stackTrace()
Automatically include the stack trace in a `console.error` output. This is useful because code frequently does: ``` try blah(); catch (e) console.log(e); ``` Which we log, but, without this, don't get the stack.
1 parent 69215e7 commit 7091b37

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/browser/console/console.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ pub const Console = struct {
6666
if (values.len == 0) {
6767
return;
6868
}
69-
log.info(.console, "error", .{ .args = try serializeValues(values, page) });
69+
70+
log.info(.console, "error", .{
71+
.args = try serializeValues(values, page),
72+
.stack = page.stackTrace() catch "???",
73+
});
7074
}
7175

7276
pub fn _clear(_: *const Console) void {}

src/browser/page.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,13 @@ pub const Page = struct {
666666
const form = (try Element._closest(element, "form", self)) orelse return null;
667667
return @ptrCast(form);
668668
}
669+
670+
pub fn stackTrace(self: *Page) !?[]const u8 {
671+
if (comptime builtin.mode == .Debug) {
672+
return self.scope.stackTrace();
673+
}
674+
return null;
675+
}
669676
};
670677

671678
const DelayedNavigation = struct {

src/runtime/js.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
601601
return persistent_object.castToObject();
602602
}
603603

604+
pub fn stackTrace(self: *const Scope) !?[]const u8 {
605+
return stackForLogs(self.call_arena, self.isolate);
606+
}
607+
604608
// Executes the src
605609
pub fn exec(self: *Scope, src: []const u8, name: ?[]const u8) !Value {
606610
const isolate = self.isolate;

0 commit comments

Comments
 (0)