Improve error stacks for async ops (#2820)

This commit is contained in:
Nayeem Rahman 2019-08-27 16:33:39 +01:00 committed by Ryan Dahl
parent 725eb98105
commit b6a4ec7d16
9 changed files with 57 additions and 39 deletions

View file

@ -15,10 +15,10 @@ interface JsonError {
interface JsonResponse {
ok?: Ok;
err?: JsonError;
promiseId?: number; // only present in async mesasges.
promiseId?: number; // Only present in async messages.
}
const promiseTable = new Map<number, util.Resolvable<number>>();
const promiseTable = new Map<number, util.Resolvable<JsonResponse>>();
let _nextPromiseId = 1;
function nextPromiseId(): number {
@ -35,25 +35,22 @@ function encode(args: object): Uint8Array {
return new TextEncoder().encode(s);
}
function toDenoError(err: JsonError): DenoError<ErrorKind> {
return new DenoError(err.kind, err.message);
function unwrapResponse(res: JsonResponse): Ok {
if (res.err != null) {
throw new DenoError(res.err!.kind, res.err!.message);
}
util.assert(res.ok != null);
return res.ok!;
}
export function asyncMsgFromRust(opId: number, res: Uint8Array): void {
const { ok, err, promiseId } = decode(res);
const promise = promiseTable.get(promiseId!)!;
if (!promise) {
throw Error(`Async op ${opId} had bad promiseId`);
}
promiseTable.delete(promiseId!);
export function asyncMsgFromRust(opId: number, resUi8: Uint8Array): void {
const res = decode(resUi8);
util.assert(res.promiseId != null);
if (err) {
promise.reject(toDenoError(err));
} else if (ok) {
promise.resolve(ok);
} else {
util.unreachable();
}
const promise = promiseTable.get(res.promiseId!);
util.assert(promise != null);
promiseTable.delete(res.promiseId!);
promise!.resolve(res);
}
export function sendSync(
@ -62,29 +59,28 @@ export function sendSync(
zeroCopy?: Uint8Array
): Ok {
const argsUi8 = encode(args);
const res = core.dispatch(opId, argsUi8, zeroCopy);
if (!res) {
return;
}
const { ok, err, promiseId } = decode(res);
util.assert(!promiseId);
if (err) {
throw toDenoError(err);
}
return ok;
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
util.assert(resUi8 != null);
const res = decode(resUi8!);
util.assert(res.promiseId == null);
return unwrapResponse(res);
}
export function sendAsync(
export async function sendAsync(
opId: number,
args: object = {},
zeroCopy?: Uint8Array
): Promise<Ok> {
const promiseId = nextPromiseId();
args = Object.assign(args, { promiseId });
const argsUi8 = encode(args);
const promise = util.createResolvable<Ok>();
promiseTable.set(promiseId, promise);
const r = core.dispatch(opId, argsUi8, zeroCopy);
util.assert(!r);
return promise;
const argsUi8 = encode(args);
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
util.assert(resUi8 == null);
const res = await promise;
return unwrapResponse(res);
}

19
js/dispatch_json_test.ts Normal file
View file

@ -0,0 +1,19 @@
import { testPerm, assertMatch, unreachable } from "./test_util.ts";
const openErrorStackPattern = new RegExp(
`^.*
at unwrapResponse \\(js\\/dispatch_json\\.ts:.*\\)
at sendAsync.* \\(js\\/dispatch_json\\.ts:.*\\)
at async Object\\.open \\(js\\/files\\.ts:.*\\).*$`,
"ms"
);
testPerm({ read: true }, async function sendAsyncStackTrace(): Promise<void> {
await Deno.open("nonexistent.txt")
.then(unreachable)
.catch(
(error): void => {
assertMatch(error.stack, openErrorStackPattern);
}
);
});

View file

@ -15,9 +15,11 @@ import {
export {
assert,
assertEquals,
assertMatch,
assertNotEquals,
assertStrictEq,
assertStrContains
assertStrContains,
unreachable
} from "./deps/https/deno.land/std/testing/asserts.ts";
interface TestPermissions {

View file

@ -13,6 +13,7 @@ import "./console_test.ts";
import "./copy_file_test.ts";
import "./custom_event_test.ts";
import "./dir_test.ts";
import "./dispatch_json_test.ts";
import "./error_stack_test.ts";
import "./event_test.ts";
import "./event_target_test.ts";

View file

@ -1,7 +1,7 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts"
[WILDCARD] js/dispatch_json.ts:[WILDCARD]
at DenoError (js/errors.ts:[WILDCARD])
at toDenoError (js/dispatch_json.ts:[WILDCARD])
at unwrapResponse (js/dispatch_json.ts:[WILDCARD])
at sendSync[WILDCARD] (js/dispatch_json.ts:[WILDCARD])
at fetchSourceFile (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])

View file

@ -1,7 +1,7 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/bad-module.ts"
[WILDCARD] js/dispatch_json.ts:[WILDCARD]
at DenoError (js/errors.ts:[WILDCARD])
at toDenoError (js/dispatch_json.ts:[WILDCARD])
at unwrapResponse (js/dispatch_json.ts:[WILDCARD])
at sendSync[WILDCARD] (js/dispatch_json.ts:[WILDCARD])
at fetchSourceFile (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])

View file

@ -1,7 +1,7 @@
[WILDCARD]error: Uncaught NotFound: Cannot resolve module "[WILDCARD]/non-existent"
[WILDCARD] js/dispatch_json.ts:[WILDCARD]
at DenoError (js/errors.ts:[WILDCARD])
at toDenoError (js/dispatch_json.ts:[WILDCARD])
at unwrapResponse (js/dispatch_json.ts:[WILDCARD])
at sendSync[WILDCARD] (js/dispatch_json.ts:[WILDCARD])
at fetchSourceFile (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])

View file

@ -1,7 +1,7 @@
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../
[WILDCARD] js/dispatch_json.ts:[WILDCARD]
at DenoError (js/errors.ts:[WILDCARD])
at toDenoError (js/dispatch_json.ts:[WILDCARD])
at unwrapResponse (js/dispatch_json.ts:[WILDCARD])
at sendSync[WILDCARD] (js/dispatch_json.ts:[WILDCARD])
at fetchSourceFile (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])

View file

@ -1,7 +1,7 @@
[WILDCARD]error: Uncaught ImportPrefixMissing: relative import path "bad-module.ts" not prefixed with / or ./ or ../
[WILDCARD] js/dispatch_json.ts:[WILDCARD]
at DenoError (js/errors.ts:[WILDCARD])
at toDenoError (js/dispatch_json.ts:[WILDCARD])
at unwrapResponse (js/dispatch_json.ts:[WILDCARD])
at sendSync[WILDCARD] (js/dispatch_json.ts:[WILDCARD])
at fetchSourceFile (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])