mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix(core): handle unregistered errors in core better (#7817)
This commit is contained in:
parent
8d00c32ee2
commit
f632b3b6e7
2 changed files with 24 additions and 16 deletions
|
@ -50,7 +50,13 @@
|
||||||
|
|
||||||
function unwrapResponse(res) {
|
function unwrapResponse(res) {
|
||||||
if (res.err != null) {
|
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;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
32
core/core.js
32
core/core.js
|
@ -196,9 +196,7 @@ SharedQueue Binary Layout
|
||||||
}
|
}
|
||||||
|
|
||||||
function getErrorClass(errorName) {
|
function getErrorClass(errorName) {
|
||||||
const className = errorMap[errorName];
|
return errorMap[errorName];
|
||||||
assert(className);
|
|
||||||
return className;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns Uint8Array
|
// Returns Uint8Array
|
||||||
|
@ -215,6 +213,20 @@ SharedQueue Binary Layout
|
||||||
let nextPromiseId = 1;
|
let nextPromiseId = 1;
|
||||||
const promiseTable = {};
|
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) {
|
async function jsonOpAsync(opName, args = {}, ...zeroCopy) {
|
||||||
setAsyncHandler(opsCache[opName], jsonOpAsyncHandler);
|
setAsyncHandler(opsCache[opName], jsonOpAsyncHandler);
|
||||||
|
|
||||||
|
@ -229,23 +241,13 @@ SharedQueue Binary Layout
|
||||||
promise.resolve = resolve;
|
promise.resolve = resolve;
|
||||||
promise.reject = reject;
|
promise.reject = reject;
|
||||||
promiseTable[args.promiseId] = promise;
|
promiseTable[args.promiseId] = promise;
|
||||||
const res = await promise;
|
return processResponse(await promise);
|
||||||
if ("ok" in res) {
|
|
||||||
return res.ok;
|
|
||||||
} else {
|
|
||||||
throw new (getErrorClass(res.err.className))(res.err.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonOpSync(opName, args = {}, ...zeroCopy) {
|
function jsonOpSync(opName, args = {}, ...zeroCopy) {
|
||||||
const argsBuf = encodeJson(args);
|
const argsBuf = encodeJson(args);
|
||||||
const res = dispatch(opName, argsBuf, ...zeroCopy);
|
const res = dispatch(opName, argsBuf, ...zeroCopy);
|
||||||
const r = decodeJson(res);
|
return processResponse(decodeJson(res));
|
||||||
if ("ok" in r) {
|
|
||||||
return r.ok;
|
|
||||||
} else {
|
|
||||||
throw new (getErrorClass(r.err.className))(r.err.message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonOpAsyncHandler(buf) {
|
function jsonOpAsyncHandler(buf) {
|
||||||
|
|
Loading…
Reference in a new issue