Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): simplify error handling #10297

Merged
merged 6 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(core): simplify error handling
- `core.js`: register builtin v8 errors in core.js so consumers don't have to
- `core.js`: remove complexity of error args handling (consumers must provide a constructor with custom args, core simply provides msg arg)
  • Loading branch information
AaronO committed Apr 21, 2021
commit 80714d502dbd5c940d54d28bd2f278424eedc85d
26 changes: 15 additions & 11 deletions core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
const { recv, send } = 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;
Expand Down Expand Up @@ -76,28 +84,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;
}
Expand Down
11 changes: 3 additions & 8 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
this = new DOMException(msg, "OperationError")
}
);
}

Expand Down