From ff9ff4a3771689c56d557bf0dbeeb46a50d1adf2 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Thu, 22 Apr 2021 02:50:50 +0200 Subject: refactor(core): simplify error handling (#10297) - register builtin v8 errors in core.js so consumers don't have to - remove complexity of error args handling (consumers must provide a constructor with custom args, core simply provides msg arg) --- core/core.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'core/core.js') diff --git a/core/core.js b/core/core.js index a86ece436..f90dbcc18 100644 --- a/core/core.js +++ b/core/core.js @@ -6,7 +6,15 @@ const { send } = window.Deno.core; let opsCache = {}; - const errorMap = {}; + const errorMap = { + // Builtin v8 / JS errors + Error, + RangeError, + ReferenceError, + SyntaxError, + TypeError, + URIError, + }; let nextPromiseId = 1; const promiseMap = new Map(); const RING_SIZE = 4 * 1024; @@ -71,28 +79,24 @@ return send(opsCache[opName], promiseId, control, zeroCopy); } - function registerErrorClass(errorName, className, args) { - if (typeof errorMap[errorName] !== "undefined") { - throw new TypeError(`Error class for "${errorName}" already registered`); + function registerErrorClass(className, errorClass) { + if (typeof errorMap[className] !== "undefined") { + throw new TypeError(`Error class for "${className}" already registered`); } - errorMap[errorName] = [className, args ?? []]; - } - - function getErrorClassAndArgs(errorName) { - return errorMap[errorName] ?? [undefined, []]; + errorMap[className] = errorClass; } function unwrapOpResult(res) { // .$err_class_name is a special key that should only exist on errors if (res?.$err_class_name) { const className = res.$err_class_name; - const [ErrorClass, args] = getErrorClassAndArgs(className); + const ErrorClass = errorMap[className]; if (!ErrorClass) { throw new Error( `Unregistered error class: "${className}"\n ${res.message}\n Classes of errors returned from ops should be registered via Deno.core.registerErrorClass().`, ); } - throw new ErrorClass(res.message, ...args); + throw new ErrorClass(res.message); } return res; } -- cgit v1.2.3