diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/errors.ts | 15 | ||||
-rw-r--r-- | js/os.ts | 16 | ||||
-rw-r--r-- | js/runtime.ts | 9 | ||||
-rw-r--r-- | js/unit_tests.ts | 15 |
4 files changed, 35 insertions, 20 deletions
diff --git a/js/errors.ts b/js/errors.ts new file mode 100644 index 000000000..2fca10eaf --- /dev/null +++ b/js/errors.ts @@ -0,0 +1,15 @@ +import { deno as fbs } from "gen/msg_generated"; + +export class DenoError<T extends fbs.ErrorKind> extends Error { + constructor(readonly kind: T, msg: string) { + super(msg); + this.name = `deno.${fbs.ErrorKind[kind]}`; + } +} + +export function maybeThrowError(base: fbs.Base): void { + const kind = base.errorKind(); + if (kind !== fbs.ErrorKind.NoError) { + throw new DenoError(kind, base.error()!); + } +} @@ -3,6 +3,7 @@ import { ModuleInfo } from "./types"; import { deno as fbs } from "gen/msg_generated"; import { assert } from "./util"; import * as util from "./util"; +import { maybeThrowError } from "./errors"; import { flatbuffers } from "flatbuffers"; import { libdeno } from "./globals"; @@ -43,9 +44,7 @@ export function codeFetch( // null assertion `!` const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!)); const baseRes = fbs.Base.getRootAsBase(bb); - if (fbs.Any.NONE === baseRes.msgType()) { - throw Error(baseRes.error()!); - } + maybeThrowError(baseRes); assert(fbs.Any.CodeFetchRes === baseRes.msgType()); const codeFetchRes = new fbs.CodeFetchRes(); assert(baseRes.msg(codeFetchRes) != null); @@ -82,10 +81,7 @@ export function codeCache( if (resBuf != null) { const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf)); const baseRes = fbs.Base.getRootAsBase(bb); - assert(fbs.Any.NONE === baseRes.msgType()); - // undefined and null are incompatible in strict mode, but at runtime - // a null value is fine, therefore not null assertion - throw Error(baseRes.error()!); + maybeThrowError(baseRes); } } @@ -112,11 +108,7 @@ export function readFileSync(filename: string): Uint8Array { // null assertion `!` const bb = new flatbuffers.ByteBuffer(new Uint8Array(resBuf!)); const baseRes = fbs.Base.getRootAsBase(bb); - if (fbs.Any.NONE === baseRes.msgType()) { - // undefined and null are incompatible in strict mode, but at runtime - // a null value is fine, therefore not null assertion - throw Error(baseRes.error()!); - } + maybeThrowError(baseRes); assert(fbs.Any.ReadFileSyncRes === baseRes.msgType()); const res = new fbs.ReadFileSyncRes(); assert(baseRes.msg(res) != null); diff --git a/js/runtime.ts b/js/runtime.ts index 1a9e8497d..93649e11c 100644 --- a/js/runtime.ts +++ b/js/runtime.ts @@ -176,14 +176,7 @@ export function resolveModule( } else { // We query Rust with a CodeFetch message. It will load the sourceCode, and // if there is any outputCode cached, will return that as well. - let fetchResponse; - try { - fetchResponse = os.codeFetch(moduleSpecifier, containingFile); - } catch (e) { - // TODO Only catch "no such file or directory" errors. Need error codes. - util.log("os.codeFetch error ignored", e.message); - return null; - } + const fetchResponse = os.codeFetch(moduleSpecifier, containingFile); filename = fetchResponse.filename; sourceCode = fetchResponse.sourceCode; outputCode = fetchResponse.outputCode; diff --git a/js/unit_tests.ts b/js/unit_tests.ts index 96341b4a2..ef65519f6 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -92,6 +92,21 @@ test(async function tests_readFileSync() { assertEqual(pkg.name, "deno"); }); +/* TODO We should be able to catch specific types. +test(function tests_readFileSync_NotFound() { + let caughtError = false; + let data; + try { + data = readFileSync("bad_filename"); + } catch (e) { + caughtError = true; + assert(e instanceof deno.NotFound); + } + assert(caughtError); + assert(data === undefined); +}); +*/ + test(async function tests_fetch() { const response = await fetch("http://localhost:4545/package.json"); const json = await response.json(); |