From f632b3b6e734670a4d21b2a5c7b23131e418077d Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Mon, 5 Oct 2020 20:35:51 +1100 Subject: [PATCH] fix(core): handle unregistered errors in core better (#7817) --- cli/rt/10_dispatch_minimal.js | 8 +++++++- core/core.js | 32 +++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cli/rt/10_dispatch_minimal.js b/cli/rt/10_dispatch_minimal.js index a843d4a9ee..dceb23e5f7 100644 --- a/cli/rt/10_dispatch_minimal.js +++ b/cli/rt/10_dispatch_minimal.js @@ -50,7 +50,13 @@ function unwrapResponse(res) { if (res.err != null) { - throw new (core.getErrorClass(res.err.className))(res.err.message); + const ErrorClass = core.getErrorClass(res.err.className); + if (!ErrorClass) { + throw new Error( + `Unregistered error class: "${res.err.className}"\n ${res.err.message}\n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass().`, + ); + } + throw new ErrorClass(res.err.message); } return res.result; } diff --git a/core/core.js b/core/core.js index 7b4e24702b..dbd7a6fd05 100644 --- a/core/core.js +++ b/core/core.js @@ -196,9 +196,7 @@ SharedQueue Binary Layout } function getErrorClass(errorName) { - const className = errorMap[errorName]; - assert(className); - return className; + return errorMap[errorName]; } // Returns Uint8Array @@ -215,6 +213,20 @@ SharedQueue Binary Layout let nextPromiseId = 1; const promiseTable = {}; + function processResponse(res) { + if ("ok" in res) { + return res.ok; + } else { + const ErrorClass = getErrorClass(res.err.className); + if (!ErrorClass) { + throw new Error( + `Unregistered error class: "${res.err.className}"\n ${res.err.message}\n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass().`, + ); + } + throw new ErrorClass(res.err.message); + } + } + async function jsonOpAsync(opName, args = {}, ...zeroCopy) { setAsyncHandler(opsCache[opName], jsonOpAsyncHandler); @@ -229,23 +241,13 @@ SharedQueue Binary Layout promise.resolve = resolve; promise.reject = reject; promiseTable[args.promiseId] = promise; - const res = await promise; - if ("ok" in res) { - return res.ok; - } else { - throw new (getErrorClass(res.err.className))(res.err.message); - } + return processResponse(await promise); } function jsonOpSync(opName, args = {}, ...zeroCopy) { const argsBuf = encodeJson(args); const res = dispatch(opName, argsBuf, ...zeroCopy); - const r = decodeJson(res); - if ("ok" in r) { - return r.ok; - } else { - throw new (getErrorClass(r.err.className))(r.err.message); - } + return processResponse(decodeJson(res)); } function jsonOpAsyncHandler(buf) {