0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

Expose deno.ErrorKind

Originally we planned to have a JS class for each error code. But it
seems better to just have a single DenoError class with a "kind"
property. One nice thing about using an enum instead of classes for
errors is that switch() can be used during error handling instead of a
bunch of instanceof branches.
This commit is contained in:
Ryan Dahl 2018-09-09 19:28:56 -04:00
parent a4f1b367b9
commit 8090fb252b
4 changed files with 23 additions and 29 deletions

View file

@ -13,5 +13,6 @@ export {
lstatSync,
writeFileSync
} from "./os";
export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";
export const argv: string[] = [];

View file

@ -1,10 +1,11 @@
import * as fbs from "gen/msg_generated";
export { ErrorKind } from "gen/msg_generated";
// @internal
export class DenoError<T extends fbs.ErrorKind> extends Error {
constructor(readonly kind: T, msg: string) {
super(msg);
this.name = `deno.${fbs.ErrorKind[kind]}`;
this.name = fbs.ErrorKind[kind];
}
}

View file

@ -15,7 +15,6 @@ test(async function fetchPerm() {
} catch (err_) {
err = err_;
}
// TODO assert(err instanceof deno.PermissionDenied).
assert(err);
assertEqual(err.name, "deno.PermissionDenied");
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});

View file

@ -16,8 +16,8 @@ test(async function envFailure() {
const env = deno.env();
} catch (err) {
caughtError = true;
// TODO assert(err instanceof deno.PermissionDenied).
assertEqual(err.name, "deno.PermissionDenied");
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
}
assert(caughtError);
@ -47,9 +47,8 @@ test(async function statSyncNotFound() {
badInfo = deno.statSync("bad_file_name");
} catch (err) {
caughtError = true;
// TODO assert(err instanceof deno.NotFound).
assert(err);
assertEqual(err.name, "deno.NotFound");
assertEqual(err.kind, deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
}
assert(caughtError);
@ -78,9 +77,8 @@ test(async function lstatSyncNotFound() {
badInfo = deno.lstatSync("bad_file_name");
} catch (err) {
caughtError = true;
// TODO assert(err instanceof deno.NotFound).
assert(err);
assertEqual(err.name, "deno.NotFound");
assertEqual(err.kind, deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
}
assert(caughtError);
@ -138,8 +136,8 @@ testPerm({ write: true }, function writeFileSyncFail() {
deno.writeFileSync(filename, data);
} catch (e) {
caughtError = true;
// TODO assertEqual(e, deno.NotFound);
assertEqual(e.name, "deno.NotFound");
assertEqual(e.kind, deno.ErrorKind.NotFound);
assertEqual(e.name, "NotFound");
}
assert(caughtError);
});
@ -166,9 +164,8 @@ testPerm({ write: true }, function makeTempDirSync() {
} catch (err_) {
err = err_;
}
// TODO assert(err instanceof deno.NotFound).
assert(err);
assertEqual(err.name, "deno.NotFound");
assertEqual(err.kind, deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
});
test(function makeTempDirSyncPerm() {
@ -179,9 +176,8 @@ test(function makeTempDirSyncPerm() {
} catch (err_) {
err = err_;
}
// TODO assert(err instanceof deno.PermissionDenied).
assert(err);
assertEqual(err.name, "deno.PermissionDenied");
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
testPerm({ write: true }, function mkdirSync() {
@ -199,9 +195,8 @@ testPerm({ write: false }, function mkdDirSyncPerm() {
} catch (err_) {
err = err_;
}
// TODO assert(err instanceof deno.PermissionDenied).
assert(err);
assertEqual(err.name, "deno.PermissionDenied");
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
testPerm({ write: true }, function renameSync() {
@ -220,9 +215,8 @@ testPerm({ write: true }, function renameSync() {
oldPathInfo = deno.statSync(oldpath);
} catch (err) {
caughtErr = true;
// TODO assert(err instanceof deno.NotFound).
assert(err);
assertEqual(err.name, "deno.NotFound");
assertEqual(err.kind, deno.ErrorKind.NotFound);
assertEqual(err.name, "NotFound");
}
assert(caughtErr);
@ -238,7 +232,6 @@ test(function renameSyncPerm() {
} catch (err_) {
err = err_;
}
// TODO assert(err instanceof deno.PermissionDenied).
assert(err);
assertEqual(err.name, "deno.PermissionDenied");
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});