1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/js/errors.ts
Ryan Dahl 8090fb252b 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.
2018-09-09 23:12:22 -04:00

27 lines
650 B
TypeScript

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 = fbs.ErrorKind[kind];
}
}
// @internal
export function maybeThrowError(base: fbs.Base): void {
const err = maybeError(base);
if (err != null) {
throw err;
}
}
export function maybeError(base: fbs.Base): null | DenoError<fbs.ErrorKind> {
const kind = base.errorKind();
if (kind === fbs.ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
}
}