diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-05-03 17:30:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-03 17:30:41 +0200 |
commit | d21380728f7d16f1a5b7362b2e2b5c46ff8a8070 (patch) | |
tree | 190e3a83254cf1d4c36ec03d4ce688cf657d27c8 /core/core.js | |
parent | 7bc03523d075ae4a5a508f9bdf59a1686f7bcdce (diff) |
fix(core): error registration could pollute constructors (#10422)
Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
Diffstat (limited to 'core/core.js')
-rw-r--r-- | core/core.js | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/core/core.js b/core/core.js index 729ca4faa..f0933d034 100644 --- a/core/core.js +++ b/core/core.js @@ -6,15 +6,15 @@ const { opcall } = window.Deno.core; let opsCache = {}; - const errorMap = { - // Builtin v8 / JS errors - Error, - RangeError, - ReferenceError, - SyntaxError, - TypeError, - URIError, - }; + const errorMap = {}; + // Builtin v8 / JS errors + registerErrorClass("Error", Error); + registerErrorClass("RangeError", RangeError); + registerErrorClass("ReferenceError", ReferenceError); + registerErrorClass("SyntaxError", SyntaxError); + registerErrorClass("TypeError", TypeError); + registerErrorClass("URIError", URIError); + let nextPromiseId = 1; const promiseMap = new Map(); const RING_SIZE = 4 * 1024; @@ -83,23 +83,27 @@ } function registerErrorClass(className, errorClass) { + registerErrorBuilder(className, (msg) => new errorClass(msg)); + } + + function registerErrorBuilder(className, errorBuilder) { if (typeof errorMap[className] !== "undefined") { throw new TypeError(`Error class for "${className}" already registered`); } - errorMap[className] = errorClass; + errorMap[className] = errorBuilder; } 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 = errorMap[className]; - if (!ErrorClass) { + const errorBuilder = errorMap[className]; + if (!errorBuilder) { 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); + throw errorBuilder(res.message); } return res; } @@ -138,6 +142,7 @@ close, print, resources, + registerErrorBuilder, registerErrorClass, handleAsyncMsgFromRust, syncOpsCache, |