Skip to content

Commit c52dce1

Browse files
authored
Merge pull request #1154 from lightpanda-io/module_evalute_error_handling
Handle (log) module evaluation errors directly
2 parents cc0c1bc + 0b4a1b4 commit c52dce1

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/browser/js/Context.zig

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,18 @@ pub fn module(self: *Context, comptime want_result: bool, src: []const u8, url:
271271
return error.ModuleInstantiationError;
272272
}
273273

274-
const evaluated = try m.evaluate(v8_context);
274+
const evaluated = m.evaluate(v8_context) catch {
275+
std.debug.assert(m.getStatus() == .kErrored);
276+
277+
// Some module-loading errors aren't handled by TryCatch. We need to
278+
// get the error from the module itself.
279+
log.warn(.js, "evaluate module", .{
280+
.specifier = owned_url,
281+
.message = self.valueToString(m.getException(), .{}) catch "???",
282+
});
283+
return error.EvaluationError;
284+
};
285+
275286
// https://v8.github.io/api/head/classv8_1_1Module.html#a1f1758265a4082595757c3251bb40e0f
276287
// Must be a promise that gets returned here.
277288
std.debug.assert(evaluated.isPromise());
@@ -1206,13 +1217,21 @@ fn _resolveModuleCallback(self: *Context, referrer: v8.Module, specifier: []cons
12061217
defer try_catch.deinit();
12071218

12081219
const entry = self.module(true, fetch_result.src(), normalized_specifier, true) catch |err| {
1209-
log.warn(.js, "compile resolved module", .{
1210-
.specifier = normalized_specifier,
1211-
.stack = try_catch.stack(self.call_arena) catch null,
1212-
.src = try_catch.sourceLine(self.call_arena) catch "err",
1213-
.line = try_catch.sourceLineNumber() orelse 0,
1214-
.exception = (try_catch.exception(self.call_arena) catch @errorName(err)) orelse @errorName(err),
1215-
});
1220+
switch (err) {
1221+
error.EvaluationError => {
1222+
// This is a sentinel value telling us that the error was already
1223+
// logged. Some module-loading errors aren't captured by Try/Catch.
1224+
// We need to handle those errors differently, where the module
1225+
// exists.
1226+
},
1227+
else => log.warn(.js, "compile resolved module", .{
1228+
.specifier = normalized_specifier,
1229+
.stack = try_catch.stack(self.call_arena) catch null,
1230+
.src = try_catch.sourceLine(self.call_arena) catch "err",
1231+
.line = try_catch.sourceLineNumber() orelse 0,
1232+
.exception = (try_catch.exception(self.call_arena) catch @errorName(err)) orelse @errorName(err),
1233+
}),
1234+
}
12161235
return null;
12171236
};
12181237
// entry.module is always set when returning from self.module()

0 commit comments

Comments
 (0)