diff options
author | Aaron O'Mullan <aaron.omullan@gmail.com> | 2021-04-22 02:50:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-21 20:50:50 -0400 |
commit | ff9ff4a3771689c56d557bf0dbeeb46a50d1adf2 (patch) | |
tree | 3c80c874f706484cfc83b148d4b8d48e5bd664fc | |
parent | 89bb774010b6b80bcbf7c19e8ed28f569abf4d90 (diff) |
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)
-rw-r--r-- | bench_util/src/js_runtime.rs | 1 | ||||
-rw-r--r-- | cli/tsc/99_main_compiler.js | 1 | ||||
-rw-r--r-- | core/benches/op_baseline.rs | 1 | ||||
-rw-r--r-- | core/core.js | 26 | ||||
-rw-r--r-- | core/examples/hello_world.rs | 4 | ||||
-rw-r--r-- | core/examples/http_bench_json_ops.js | 1 | ||||
-rw-r--r-- | core/ops_json.rs | 2 | ||||
-rw-r--r-- | op_crates/url/benches/url_ops.rs | 1 | ||||
-rw-r--r-- | runtime/js/99_main.js | 11 |
9 files changed, 18 insertions, 30 deletions
diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs index 1467ee077..415f4a135 100644 --- a/bench_util/src/js_runtime.rs +++ b/bench_util/src/js_runtime.rs @@ -20,7 +20,6 @@ pub fn create_js_runtime(setup: impl FnOnce(&mut JsRuntime)) -> JsRuntime { "init", r#" Deno.core.ops(); - Deno.core.registerErrorClass('Error', Error); "#, ) .unwrap(); diff --git a/cli/tsc/99_main_compiler.js b/cli/tsc/99_main_compiler.js index 73bab249e..ebb4d679d 100644 --- a/cli/tsc/99_main_compiler.js +++ b/cli/tsc/99_main_compiler.js @@ -799,7 +799,6 @@ delete Object.prototype.__proto__; // Setup the compiler runtime during the build process. core.ops(); - core.registerErrorClass("Error", Error); // A build time only op that provides some setup information that is used to // ensure the snapshot is setup properly. diff --git a/core/benches/op_baseline.rs b/core/benches/op_baseline.rs index 132d92f00..04b72959c 100644 --- a/core/benches/op_baseline.rs +++ b/core/benches/op_baseline.rs @@ -27,7 +27,6 @@ fn create_js_runtime() -> JsRuntime { "init", r#" Deno.core.ops(); - Deno.core.registerErrorClass('Error', Error); "#, ) .unwrap(); 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; } diff --git a/core/examples/hello_world.rs b/core/examples/hello_world.rs index 154c05d97..3a40ee29a 100644 --- a/core/examples/hello_world.rs +++ b/core/examples/hello_world.rs @@ -74,10 +74,6 @@ const _newline = new Uint8Array([10]); function print(value) { Deno.core.dispatchByName('op_print', 0, value.toString(), _newline); } - -// Finally we register the error class used by op_sum -// so that it throws the correct class. -Deno.core.registerErrorClass('Error', Error); "#, ) .unwrap(); diff --git a/core/examples/http_bench_json_ops.js b/core/examples/http_bench_json_ops.js index 687be7ec1..672747196 100644 --- a/core/examples/http_bench_json_ops.js +++ b/core/examples/http_bench_json_ops.js @@ -55,7 +55,6 @@ async function serve(rid) { async function main() { Deno.core.ops(); - Deno.core.registerErrorClass("Error", Error); const listenerRid = listen(); Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4544/\n`); diff --git a/core/ops_json.rs b/core/ops_json.rs index d368453d9..309fac12d 100644 --- a/core/ops_json.rs +++ b/core/ops_json.rs @@ -122,8 +122,6 @@ mod tests { r#" // First we initialize the ops cache. This maps op names to their id's. Deno.core.ops(); - // Register the error class. - Deno.core.registerErrorClass('Error', Error); async function f1() { await Deno.core.opAsync('op_throw', 'hello'); diff --git a/op_crates/url/benches/url_ops.rs b/op_crates/url/benches/url_ops.rs index 17029b9c6..37efb0a33 100644 --- a/op_crates/url/benches/url_ops.rs +++ b/op_crates/url/benches/url_ops.rs @@ -28,7 +28,6 @@ fn create_js_runtime() -> JsRuntime { "init", r#" Deno.core.ops(); - Deno.core.registerErrorClass('Error', Error); "#, ) .unwrap(); diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 7742e2ba2..851d798c3 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -186,16 +186,11 @@ delete Object.prototype.__proto__; core.registerErrorClass("Http", errors.Http); core.registerErrorClass("Busy", errors.Busy); core.registerErrorClass("NotSupported", errors.NotSupported); - core.registerErrorClass("Error", Error); - core.registerErrorClass("RangeError", RangeError); - core.registerErrorClass("ReferenceError", ReferenceError); - core.registerErrorClass("SyntaxError", SyntaxError); - core.registerErrorClass("TypeError", TypeError); - core.registerErrorClass("URIError", URIError); core.registerErrorClass( "DOMExceptionOperationError", - DOMException, - "OperationError", + function DOMExceptionOperationError(msg) { + DOMException.prototype.constructor.call(this, msg, "OperationError"); + }, ); } |