2019-01-22 04:03:30 +09:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-08 10:33:30 +11:00
|
|
|
import { Base, ErrorKind } from "gen/msg_generated";
|
2018-09-09 19:28:56 -04:00
|
|
|
export { ErrorKind } from "gen/msg_generated";
|
2018-08-15 23:36:48 -04:00
|
|
|
|
2018-10-15 07:29:50 +11:00
|
|
|
/** A Deno specific error. The `kind` property is set to a specific error code
|
|
|
|
* which can be used to in application logic.
|
|
|
|
*
|
2019-02-13 02:08:56 +11:00
|
|
|
* try {
|
|
|
|
* somethingThatMightThrow();
|
|
|
|
* } catch (e) {
|
|
|
|
* if (
|
|
|
|
* e instanceof Deno.DenoError &&
|
|
|
|
* e.kind === Deno.ErrorKind.Overflow
|
|
|
|
* ) {
|
|
|
|
* console.error("Overflow error!");
|
|
|
|
* }
|
2018-10-15 07:29:50 +11:00
|
|
|
* }
|
2019-02-13 02:08:56 +11:00
|
|
|
*
|
2018-10-15 07:29:50 +11:00
|
|
|
*/
|
2018-10-08 10:33:30 +11:00
|
|
|
export class DenoError<T extends ErrorKind> extends Error {
|
|
|
|
constructor(readonly kind: T, msg: string) {
|
|
|
|
super(msg);
|
|
|
|
this.name = ErrorKind[kind];
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 10:33:30 +11:00
|
|
|
// @internal
|
|
|
|
export function maybeError(base: Base): null | DenoError<ErrorKind> {
|
2018-08-15 23:36:48 -04:00
|
|
|
const kind = base.errorKind();
|
2018-10-08 10:33:30 +11:00
|
|
|
if (kind === ErrorKind.NoError) {
|
2018-09-05 22:13:36 -04:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return new DenoError(kind, base.error()!);
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-10 04:30:38 +11:00
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function maybeThrowError(base: Base): void {
|
|
|
|
const err = maybeError(base);
|
|
|
|
if (err != null) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|