-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
errors.ts
54 lines (46 loc) · 1.38 KB
/
errors.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { format } from 'util';
export class CheckFailed extends Error {
constructor(message: string, readonly exitCode: number = 1) {
super(message);
}
}
export class ApplicationError extends Error {
constructor(message: string, readonly exitCode: number = 1, readonly cause?: Error) {
super(message);
}
}
export class IOError extends ApplicationError {
constructor(message: string, readonly cause: NodeError) {
super(message, undefined, cause);
}
get code(): string | undefined {
return this.cause.code;
}
isNotFound() {
return this.cause.code === 'ENOENT';
}
}
export function toError(e: unknown): NodeError {
if (isError(e)) return e;
const message = format(e);
return {
name: 'error',
message,
toString: () => message,
};
}
export function isError(e: unknown): e is NodeError {
if (e instanceof Error) return true;
if (!e || typeof e !== 'object') return false;
const ex = <Error>e;
return typeof ex.message === 'string';
}
export function toApplicationError(e: unknown, message?: string): ApplicationError {
if (e instanceof ApplicationError && !message) return e;
const err = toError(e);
return new ApplicationError(message ?? err.message, undefined, err);
}
export interface NodeError extends Error {
code?: string;
toString?: () => string;
}