summaryrefslogtreecommitdiff
path: root/runtime/js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2022-06-06 19:26:57 +0100
committerGitHub <noreply@github.com>2022-06-06 20:26:57 +0200
commite3eae662f3d753141571bd132ccb199f95c745ea (patch)
treee0cdad78f409c9b221b909d0d7dfcee286e6325f /runtime/js
parent1081659be176a59512a7e9e3dc93e13046a26aec (diff)
fix: Format non-error exceptions (#14604)
This commit adds "Deno.core.setFormatExceptionCallback" which can be used to provide custom formatting for errors. It is useful in cases when user throws something that is non-Error (eg. a string, plain object, etc).
Diffstat (limited to 'runtime/js')
-rw-r--r--runtime/js/40_testing.js6
-rw-r--r--runtime/js/99_main.js19
2 files changed, 23 insertions, 2 deletions
diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js
index 8c7d69fb0..865482042 100644
--- a/runtime/js/40_testing.js
+++ b/runtime/js/40_testing.js
@@ -217,14 +217,16 @@
let msg = `Test case is leaking async ops.
-- ${ArrayPrototypeJoin(details, "\n - ")}`;
+ - ${ArrayPrototypeJoin(details, "\n - ")}`;
if (!core.isOpCallTracingEnabled()) {
msg +=
`\n\nTo get more details where ops were leaked, run again with --trace-ops flag.`;
+ } else {
+ msg += "\n";
}
- throw msg;
+ throw assert(false, msg);
};
}
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index b469a29dc..0bacaf58c 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -38,6 +38,8 @@ delete Object.prototype.__proto__;
const encoding = window.__bootstrap.encoding;
const colors = window.__bootstrap.colors;
const Console = window.__bootstrap.console.Console;
+ const inspectArgs = window.__bootstrap.console.inspectArgs;
+ const quoteString = window.__bootstrap.console.quoteString;
const compression = window.__bootstrap.compression;
const worker = window.__bootstrap.worker;
const internals = window.__bootstrap.internals;
@@ -210,9 +212,26 @@ delete Object.prototype.__proto__;
return core.opSync("op_main_module");
}
+ function formatException(error) {
+ if (error instanceof Error) {
+ return null;
+ } else if (typeof error == "string") {
+ return `Uncaught ${
+ inspectArgs([quoteString(error)], {
+ colors: !colors.getNoColor(),
+ })
+ }`;
+ } else {
+ return `Uncaught ${
+ inspectArgs([error], { colors: !colors.getNoColor() })
+ }`;
+ }
+ }
+
function runtimeStart(runtimeOptions, source) {
core.setMacrotaskCallback(timers.handleTimerMacrotask);
core.setWasmStreamingCallback(fetch.handleWasmStreaming);
+ core.setFormatExceptionCallback(formatException);
version.setVersions(
runtimeOptions.denoVersion,
runtimeOptions.v8Version,