summaryrefslogtreecommitdiff
path: root/core/01_core.js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2022-08-21 19:16:42 +0100
committerGitHub <noreply@github.com>2022-08-21 20:16:42 +0200
commit97954003cc87b664768918173e8d00f6df35e04f (patch)
treedcad94ac57503ba56e8cd14847cc60b1c2ddc91c /core/01_core.js
parente96933bc163fd81a276cbc169b17f76724a5ac33 (diff)
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).
Diffstat (limited to 'core/01_core.js')
-rw-r--r--core/01_core.js30
1 files changed, 29 insertions, 1 deletions
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"),