1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-14 16:33:45 -05:00
denoland-deno/js/errors.ts

28 lines
656 B
TypeScript
Raw Normal View History

2018-10-03 21:18:23 -04:00
import * as msg from "gen/msg_generated";
export { ErrorKind } from "gen/msg_generated";
// @internal
2018-10-03 21:18:23 -04:00
export class DenoError<T extends msg.ErrorKind> extends Error {
constructor(readonly kind: T, errStr: string) {
super(errStr);
this.name = msg.ErrorKind[kind];
}
}
// @internal
2018-10-03 21:18:23 -04:00
export function maybeThrowError(base: msg.Base): void {
const err = maybeError(base);
if (err != null) {
throw err;
}
}
2018-10-03 21:18:23 -04:00
export function maybeError(base: msg.Base): null | DenoError<msg.ErrorKind> {
const kind = base.errorKind();
2018-10-03 21:18:23 -04:00
if (kind === msg.ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
}
}