From 97954003cc87b664768918173e8d00f6df35e04f Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 21 Aug 2022 19:16:42 +0100 Subject: feat: `queueMicrotask()` error handling (#15522) Adds error event dispatching for queueMicrotask(). Consequently unhandled errors are now reported with Deno.core.terminate(), which is immune to the existing quirk with plainly thrown errors (#14158). --- core/01_core.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/01_core.js b/core/01_core.js index aaff59148..e1ac275eb 100644 --- a/core/01_core.js +++ b/core/01_core.js @@ -210,8 +210,35 @@ return aggregate; } + let reportExceptionCallback = undefined; + + // Used to report errors thrown from functions passed to `queueMicrotask()`. + // The callback will be passed the thrown error. For example, you can use this + // to dispatch an error event to the global scope. + // In other words, set the implementation for + // https://html.spec.whatwg.org/multipage/webappapis.html#report-the-exception + function setReportExceptionCallback(cb) { + if (typeof cb != "function") { + throw new TypeError("expected a function"); + } + reportExceptionCallback = cb; + } + function queueMicrotask(cb) { - return ops.op_queue_microtask(cb); + if (typeof cb != "function") { + throw new TypeError("expected a function"); + } + return ops.op_queue_microtask(() => { + try { + cb(); + } catch (error) { + if (reportExceptionCallback) { + reportExceptionCallback(error); + } else { + throw error; + } + } + }); } // Some "extensions" rely on "BadResource" and "Interrupted" errors in the @@ -252,6 +279,7 @@ opCallTraces, refOp, unrefOp, + setReportExceptionCallback, close: (rid) => ops.op_close(rid), tryClose: (rid) => ops.op_try_close(rid), read: opAsync.bind(null, "op_read"), -- cgit v1.2.3