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.
This commit is contained in:
Karl Seguin
2025-06-03 20:40:40 +08:00
parent 69215e7d27
commit 7091b37f3a
3 changed files with 16 additions and 1 deletions

View File

@@ -66,7 +66,11 @@ pub const Console = struct {
if (values.len == 0) {
return;
}
log.info(.console, "error", .{ .args = try serializeValues(values, page) });
log.info(.console, "error", .{
.args = try serializeValues(values, page),
.stack = page.stackTrace() catch "???",
});
}
pub fn _clear(_: *const Console) void {}

View File

@@ -666,6 +666,13 @@ pub const Page = struct {
const form = (try Element._closest(element, "form", self)) orelse return null;
return @ptrCast(form);
}
pub fn stackTrace(self: *Page) !?[]const u8 {
if (comptime builtin.mode == .Debug) {
return self.scope.stackTrace();
}
return null;
}
};
const DelayedNavigation = struct {

View File

@@ -601,6 +601,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
return persistent_object.castToObject();
}
pub fn stackTrace(self: *const Scope) !?[]const u8 {
return stackForLogs(self.call_arena, self.isolate);
}
// Executes the src
pub fn exec(self: *Scope, src: []const u8, name: ?[]const u8) !Value {
const isolate = self.isolate;