summaryrefslogtreecommitdiff
path: root/js/errors.ts
blob: d6e9713cf456e13e8a752d2d0a1af0be8200d824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { Base, ErrorKind } from "gen/msg_generated";
export { ErrorKind } from "gen/msg_generated";

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()!);
  }
}