1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/js/errors.ts
Ryan Dahl c9614d86c1
Move //libdeno to //core/libdeno (#2015)
Fixes some sed errors introduced in c43cfe.

Unfortunately moving libdeno required splitting build.rs into two parts,
one for cli and one for core.

I've also removed the arm64 build - it's complicating things at this
re-org and we're not even testing it. I need to swing back to it and get
tools/test.py running for it.
2019-03-30 14:45:36 -04:00

43 lines
1.1 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { Base, ErrorKind } from "gen/cli/msg_generated";
export { ErrorKind } from "gen/cli/msg_generated";
/** A Deno specific error. The `kind` property is set to a specific error code
* which can be used to in application logic.
*
* try {
* somethingThatMightThrow();
* } catch (e) {
* if (
* e instanceof Deno.DenoError &&
* e.kind === Deno.ErrorKind.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 maybeError(base: Base): null | DenoError<ErrorKind> {
const kind = base.errorKind();
if (kind === ErrorKind.NoError) {
return null;
} else {
return new DenoError(kind, base.error()!);
}
}
// @internal
export function maybeThrowError(base: Base): void {
const err = maybeError(base);
if (err != null) {
throw err;
}
}