1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-04 17:18:23 -05:00
denoland-deno/js/errors.ts

40 lines
1 KiB
TypeScript
Raw Normal View History

import { Base, ErrorKind } from "gen/msg_generated";
export { ErrorKind } from "gen/msg_generated";
2018-10-14 16:29:50 -04:00
/** A Deno specific error. The `kind` property is set to a specific error code
* which can be used to in application logic.
*
* import { DenoError, ErrorKind } from "deno";
* try {
* somethingThatMightThrow();
* } catch (e) {
* if (e instanceof DenoError && e.kind === DenoError.Overflow) {
* console.error("Overflow error!");
* }
* }
*/
export class DenoError<T extends ErrorKind> extends Error {
constructor(readonly kind: T, msg: string) {
super(msg);
this.name = ErrorKind[kind];
}
}
// @internal
export function maybeThrowError(base: Base): void {
const err = maybeError(base);
if (err != null) {
throw err;
}
}
// @internal
export function maybeError(base: Base): null | DenoError<ErrorKind> {
const kind = base.errorKind();
if (kind === ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
}
}