2018-10-03 21:18:23 -04:00
|
|
|
import * as msg 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-09-04 15:23:38 -04:00
|
|
|
// @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];
|
2018-08-15 23:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:23:38 -04:00
|
|
|
// @internal
|
2018-10-03 21:18:23 -04:00
|
|
|
export function maybeThrowError(base: msg.Base): void {
|
2018-09-05 22:13:36 -04:00
|
|
|
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> {
|
2018-08-15 23:36:48 -04:00
|
|
|
const kind = base.errorKind();
|
2018-10-03 21:18:23 -04:00
|
|
|
if (kind === msg.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
|
|
|
}
|
|
|
|
}
|