-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
_errors.ts
206 lines (180 loc) 路 5.77 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { didYouMeanCommand } from "./_utils.ts";
import type { Command } from "./command.ts";
import { getFlag } from "../flags/_utils.ts";
import { IEnvVar } from "./types.ts";
export class CommandError extends Error {
constructor(message: string) {
super(message);
Object.setPrototypeOf(this, CommandError.prototype);
}
}
export interface ValidationErrorOptions {
exitCode?: number;
}
export class ValidationError extends CommandError {
public readonly exitCode: number;
constructor(message: string, { exitCode }: ValidationErrorOptions = {}) {
super(message);
Object.setPrototypeOf(this, ValidationError.prototype);
this.exitCode = exitCode ?? 1;
}
}
export class DuplicateOptionName extends CommandError {
constructor(name: string) {
super(`Option with name "${getFlag(name)}" already exists.`);
Object.setPrototypeOf(this, DuplicateOptionName.prototype);
}
}
export class MissingCommandName extends CommandError {
constructor() {
super("Missing command name.");
Object.setPrototypeOf(this, MissingCommandName.prototype);
}
}
export class DuplicateCommandName extends CommandError {
constructor(name: string) {
super(`Duplicate command name "${name}".`);
Object.setPrototypeOf(this, DuplicateCommandName.prototype);
}
}
export class DuplicateCommandAlias extends CommandError {
constructor(alias: string) {
super(`Duplicate command alias "${alias}".`);
Object.setPrototypeOf(this, DuplicateCommandAlias.prototype);
}
}
export class CommandNotFound extends CommandError {
constructor(
name: string,
commands: Array<Command>,
excluded?: Array<string>,
) {
super(
`Unknown command "${name}".${
didYouMeanCommand(name, commands, excluded)
}`,
);
Object.setPrototypeOf(this, UnknownCommand.prototype);
}
}
export class DuplicateType extends CommandError {
constructor(name: string) {
super(`Type with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateType.prototype);
}
}
export class DuplicateCompletion extends CommandError {
constructor(name: string) {
super(`Completion with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateCompletion.prototype);
}
}
export class DuplicateExample extends CommandError {
constructor(name: string) {
super(`Example with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateExample.prototype);
}
}
export class DuplicateEnvironmentVariable extends CommandError {
constructor(name: string) {
super(`Environment variable with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateEnvironmentVariable.prototype);
}
}
export class MissingRequiredEnvVar extends ValidationError {
constructor(envVar: IEnvVar) {
super(`Missing required environment variable "${envVar.names[0]}".`);
Object.setPrototypeOf(this, MissingRequiredEnvVar.prototype);
}
}
export class EnvironmentVariableSingleValue extends CommandError {
constructor(name: string) {
super(
`An environment variable can only have one value, but "${name}" has more than one.`,
);
Object.setPrototypeOf(this, EnvironmentVariableSingleValue.prototype);
}
}
export class EnvironmentVariableOptionalValue extends CommandError {
constructor(name: string) {
super(
`An environment variable cannot have an optional value, but "${name}" is defined as optional.`,
);
Object.setPrototypeOf(this, EnvironmentVariableOptionalValue.prototype);
}
}
export class EnvironmentVariableVariadicValue extends CommandError {
constructor(name: string) {
super(
`An environment variable cannot have an variadic value, but "${name}" is defined as variadic.`,
);
Object.setPrototypeOf(this, EnvironmentVariableVariadicValue.prototype);
}
}
export class DefaultCommandNotFound extends CommandError {
constructor(name: string, commands: Array<Command>) {
super(
`Default command "${name}" not found.${
didYouMeanCommand(name, commands)
}`,
);
Object.setPrototypeOf(this, DefaultCommandNotFound.prototype);
}
}
export class CommandExecutableNotFound extends CommandError {
constructor(name: string) {
super(
`Command executable not found: ${name}`,
);
Object.setPrototypeOf(this, CommandExecutableNotFound.prototype);
}
}
export class UnknownCompletionCommand extends CommandError {
constructor(name: string, commands: Array<Command>) {
super(
`Auto-completion failed. Unknown command "${name}".${
didYouMeanCommand(name, commands)
}`,
);
Object.setPrototypeOf(this, UnknownCompletionCommand.prototype);
}
}
/* Validation errors. */
export class UnknownCommand extends ValidationError {
constructor(
name: string,
commands: Array<Command>,
excluded?: Array<string>,
) {
super(
`Unknown command "${name}".${
didYouMeanCommand(name, commands, excluded)
}`,
);
Object.setPrototypeOf(this, UnknownCommand.prototype);
}
}
export class NoArgumentsAllowed extends ValidationError {
constructor(name: string) {
super(`No arguments allowed for command "${name}".`);
Object.setPrototypeOf(this, NoArgumentsAllowed.prototype);
}
}
export class MissingArguments extends ValidationError {
constructor(args: Array<string>) {
super("Missing argument(s): " + args.join(", "));
Object.setPrototypeOf(this, MissingArguments.prototype);
}
}
export class MissingArgument extends ValidationError {
constructor(arg: string) {
super(`Missing argument "${arg}".`);
Object.setPrototypeOf(this, MissingArgument.prototype);
}
}
export class TooManyArguments extends ValidationError {
constructor(args: Array<string>) {
super(`Too many arguments: ${args.join(" ")}`);
Object.setPrototypeOf(this, TooManyArguments.prototype);
}
}