forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
74 lines (59 loc) · 1.95 KB
/
errors.ts
File metadata and controls
74 lines (59 loc) · 1.95 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import chalk from 'chalk';
export interface CLIError extends Error {
code: string;
exitCode: number;
}
export class ValidationError extends Error implements CLIError {
code: string;
exitCode: number;
constructor(message: string, code: string = 'E4001', exitCode: number = 1) {
super(message);
this.name = 'ValidationError';
this.code = code;
this.exitCode = exitCode;
}
}
export class ConfigurationError extends Error implements CLIError {
code: string;
exitCode: number;
constructor(message: string, code: string = 'E4002', exitCode: number = 1) {
super(message);
this.name = 'ConfigurationError';
this.code = code;
this.exitCode = exitCode;
}
}
export class RuntimeError extends Error implements CLIError {
code: string;
exitCode: number;
constructor(message: string, code: string = 'E5001', exitCode: number = 1) {
super(message);
this.name = 'RuntimeError';
this.code = code;
this.exitCode = exitCode;
}
}
const getErrorDetails = (error: Error | CLIError): { errorCode: string; exitCode: number } => {
const isCLIError = 'code' in error && 'exitCode' in error;
const cliError = error as CLIError;
return {
errorCode: isCLIError ? cliError.code : 'E9999',
exitCode: isCLIError ? cliError.exitCode : 1,
};
};
const logErrorStack = (error: Error): void => {
const isDebugMode = process.env.DEBUG === 'auto-engineer';
if (isDebugMode && error.stack != null && error.stack.length > 0) {
console.error(chalk.gray('Stack trace:'));
console.error(chalk.gray(error.stack));
}
};
export const handleError = (error: Error | CLIError): never => {
const { errorCode, exitCode } = getErrorDetails(error);
console.error(chalk.red(`Error (${errorCode}): ${error.message}`));
logErrorStack(error);
process.exit(exitCode);
};
export const createError = (message: string, code: string, exitCode: number = 1): CLIError => {
return new ValidationError(message, code, exitCode);
};