-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-errors.ts
More file actions
29 lines (26 loc) · 1.17 KB
/
cli-errors.ts
File metadata and controls
29 lines (26 loc) · 1.17 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
import Cli from ".";
/** Error messages for each error type */
export const ERROR_MESSAGES = {
command_not_found: 'Command "{command}" not found',
option_not_found: 'Unknown option "{option}"',
option_wrong_value: 'Wrong value for option "{option}". Expected {expected} but found "{found}"',
option_missing_value: 'Missing value of type <{type}> for option "{option}"',
option_required: 'Missing required option "{option}"',
option_missing_dependencies: 'Missing dependencies for option "{option}": {dependencies}',
} as const;
/** Existing errors */
export type ErrorType = keyof typeof ERROR_MESSAGES;
/** Utility class to identify error messages */
export class CliError {
/** Test if the given error message matches an error type */
private static test(value: string, error: string) {
return new RegExp(error.replace(/\{\w+\}/g, "[a-zA-Z-0-9/\\., \"'|]+")).test(value);
}
/** Analize the given error message to identify its type */
static analize(value: string | undefined): ErrorType | undefined {
if (!value) {
return undefined;
}
return Object.entries(Cli.messages).find(([_, message]) => this.test(value, message))?.[0] as ErrorType;
}
}