Skip to content

Commit d50f761

Browse files
committed
event: log listeners errors and continue execution
1 parent 23dafb0 commit d50f761

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/netsurf.zig

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ const c = @cImport({
2929
const mimalloc = @import("mimalloc.zig");
3030

3131
const Callback = @import("jsruntime").Callback;
32+
const CallbackResult = @import("jsruntime").CallbackResult;
3233
const EventToInterface = @import("events/event.zig").Event.toInterface;
3334

35+
const log = std.log.scoped(.netsurf);
36+
3437
// init initializes netsurf lib.
3538
// init starts a mimalloc heap arena for the netsurf session. The caller must
3639
// call deinit() to free the arena memory.
@@ -534,13 +537,24 @@ const event_handler = struct {
534537
if (data) |d| {
535538
const func = event_handler_cbk(d);
536539

540+
// TODO get the allocator by another way?
541+
var res = CallbackResult.init(func.nat_ctx.alloc);
542+
defer res.deinit();
543+
537544
if (event) |evt| {
538-
func.call(.{
545+
func.trycall(.{
539546
EventToInterface(evt) catch unreachable,
540-
}) catch unreachable;
547+
}, &res) catch {};
541548
} else {
542-
func.call(.{event}) catch unreachable;
549+
func.trycall(.{event}, &res) catch {};
543550
}
551+
552+
// in case of function error, we log the result and the trace.
553+
if (!res.success) {
554+
log.info("event handler error: {s}", .{res.result orelse "unknown"});
555+
log.debug("{s}", .{res.stack orelse "no stack trace"});
556+
}
557+
544558
// NOTE: we can not call func.deinit here
545559
// b/c the handler can be called several times
546560
// either on this dispatch event or in anoter one

0 commit comments

Comments
 (0)