summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorNayeem Rahman <muhammed.9939@gmail.com>2019-08-27 16:33:39 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-08-27 11:33:39 -0400
commitb6a4ec7d163810d52750f04ec2073b13f8943991 (patch)
tree5ad6f92fe6ec3b859e0c50274e28894f312ea7dd /js
parent725eb9810590e8237df68ec5580daaf0fa77e7d3 (diff)
Improve error stacks for async ops (#2820)
Diffstat (limited to 'js')
-rw-r--r--js/dispatch_json.ts62
-rw-r--r--js/dispatch_json_test.ts19
-rw-r--r--js/test_util.ts4
-rw-r--r--js/unit_tests.ts1
4 files changed, 52 insertions, 34 deletions
diff --git a/js/dispatch_json.ts b/js/dispatch_json.ts
index bde2b7cb1..8ec924f4c 100644
--- a/js/dispatch_json.ts
+++ b/js/dispatch_json.ts
@@ -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);
}
diff --git a/js/dispatch_json_test.ts b/js/dispatch_json_test.ts
new file mode 100644
index 000000000..47e5cef2f
--- /dev/null
+++ b/js/dispatch_json_test.ts
@@ -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);
+ }
+ );
+});
diff --git a/js/test_util.ts b/js/test_util.ts
index 454f26ff2..bcbacf281 100644
--- a/js/test_util.ts
+++ b/js/test_util.ts
@@ -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 {
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index b55c1954a..711a092fd 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -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";