Skip to content

Commit d8718a9

Browse files
committed
include stack trace in JS function call log errors
1 parent 5f92b52 commit d8718a9

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/runtime/js.zig

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,8 +1857,10 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18571857

18581858
fn generateNamedIndexer(comptime Struct: type, template_proto: v8.ObjectTemplate) void {
18591859
if (@hasDecl(Struct, "named_get") == false) {
1860-
if (builtin.mode == .Debug and log.enabled(.unknown_prop, .debug)) {
1861-
generateDebugNamedIndexer(Struct, template_proto);
1860+
if (comptime builtin.mode == .Debug) {
1861+
if (log.enabled(.unknown_prop, .debug)) {
1862+
generateDebugNamedIndexer(Struct, template_proto);
1863+
}
18621864
}
18631865
return;
18641866
}
@@ -2499,16 +2501,20 @@ fn Caller(comptime E: type, comptime State: type) type {
24992501
}
25002502

25012503
fn handleError(self: *Self, comptime Struct: type, comptime named_function: NamedFunction, err: anyerror, info: anytype) void {
2502-
if (builtin.mode == .Debug and log.enabled(.js, .warn) and @hasDecl(@TypeOf(info), "length")) {
2503-
const args_dump = self.serializeFunctionArgs(info) catch "failed to serialize args";
2504-
log.warn(.js, "function call error", .{
2505-
.name = named_function.full_name,
2506-
.err = err,
2507-
.args = args_dump,
2508-
});
2504+
const isolate = self.isolate;
2505+
2506+
if (comptime builtin.mode == .Debug and @hasDecl(@TypeOf(info), "length")) {
2507+
if (log.enabled(.js, .warn)) {
2508+
const args_dump = self.serializeFunctionArgs(info) catch "failed to serialize args";
2509+
log.warn(.js, "function call error", .{
2510+
.name = named_function.full_name,
2511+
.err = err,
2512+
.args = args_dump,
2513+
.stack = stackForLogs(self.call_arena, isolate) catch |err1| @errorName(err1),
2514+
});
2515+
}
25092516
}
25102517

2511-
const isolate = self.isolate;
25122518
var js_err: ?v8.Value = switch (err) {
25132519
error.InvalidArgument => createTypeException(isolate, "invalid argument"),
25142520
error.OutOfMemory => createException(isolate, "out of memory"),
@@ -3105,6 +3111,24 @@ fn jsStringToZig(allocator: Allocator, str: v8.String, isolate: v8.Isolate) ![]u
31053111
return buf;
31063112
}
31073113

3114+
fn stackForLogs(arena: Allocator, isolate: v8.Isolate) !?[]const u8 {
3115+
std.debug.assert(builtin.mode == .Debug);
3116+
3117+
const separator = log.separator();
3118+
var buf: std.ArrayListUnmanaged(u8) = .empty;
3119+
var writer = buf.writer(arena);
3120+
3121+
const stack_trace = v8.StackTrace.getCurrentStackTrace(isolate, 30);
3122+
const frame_count = stack_trace.getFrameCount();
3123+
3124+
for (0..frame_count) |i| {
3125+
const frame = stack_trace.getFrame(isolate, @intCast(i));
3126+
const script = try jsStringToZig(arena, frame.getScriptName(), isolate);
3127+
try writer.print("{s}{s}:{d}", .{ separator, script, frame.getLineNumber() });
3128+
}
3129+
return buf.items;
3130+
}
3131+
31083132
const NoopInspector = struct {
31093133
pub fn onInspectorResponse(_: *anyopaque, _: u32, _: []const u8) void {}
31103134
pub fn onInspectorEvent(_: *anyopaque, _: []const u8) void {}

0 commit comments

Comments
 (0)