-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy path_errors.ts
227 lines (201 loc) · 6.59 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { didYouMeanCommand } from "./_utils.ts";
import type { Command } from "./command.ts";
import { getFlag } from "./_utils.ts";
import { bold } from "@std/fmt/colors";
import type { EnvVar } 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;
public cmd?: Command;
constructor(message: string, { exitCode }: ValidationErrorOptions = {}) {
super(message);
Object.setPrototypeOf(this, ValidationError.prototype);
this.exitCode = exitCode ?? 2;
}
}
export class DuplicateOptionNameError extends CommandError {
constructor(optionName: string, commandName: string) {
super(
`An option with name '${
bold(getFlag(optionName))
}' is already registered on command '${
bold(commandName)
}'. If it is intended to override the option, set the '${
bold("override")
}' option of the '${bold("option")}' method to true.`,
);
Object.setPrototypeOf(this, DuplicateOptionNameError.prototype);
}
}
export class MissingCommandNameCompletionsError extends CommandError {
constructor(shell: string) {
super(
`Failed to generate shell completions. Missing main command name. Use '${
bold('cmd.name("<comand-name>")')
}' to set the name of the main command or use the '${
bold("--name")
}' option from the '${
bold("completions")
}' command to set the cli name: '${
bold(`<command> completions ${shell} --name <cli-name>`)
}'.`,
);
Object.setPrototypeOf(
this,
MissingCommandNameCompletionsError.prototype,
);
}
}
export class MissingCommandNameError extends CommandError {
constructor() {
super("Missing command name.");
Object.setPrototypeOf(this, MissingCommandNameError.prototype);
}
}
export class DuplicateCommandNameError extends CommandError {
constructor(name: string) {
super(`Duplicate command name "${name}".`);
Object.setPrototypeOf(this, DuplicateCommandNameError.prototype);
}
}
export class DuplicateCommandAliasError extends CommandError {
constructor(alias: string) {
super(`Duplicate command alias "${alias}".`);
Object.setPrototypeOf(this, DuplicateCommandAliasError.prototype);
}
}
export class CommandNotFoundError extends CommandError {
constructor(
name: string,
commands: Array<Command>,
excluded?: Array<string>,
) {
super(
`Unknown command "${name}".${
didYouMeanCommand(name, commands, excluded)
}`,
);
Object.setPrototypeOf(this, CommandNotFoundError.prototype);
}
}
export class DuplicateTypeError extends CommandError {
constructor(name: string) {
super(`Type with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateTypeError.prototype);
}
}
export class DuplicateCompletionError extends CommandError {
constructor(name: string) {
super(`Completion with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateCompletionError.prototype);
}
}
export class DuplicateExampleError extends CommandError {
constructor(name: string) {
super(`Example with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateExampleError.prototype);
}
}
export class DuplicateEnvVarError extends CommandError {
constructor(name: string) {
super(`Environment variable with name "${name}" already exists.`);
Object.setPrototypeOf(this, DuplicateEnvVarError.prototype);
}
}
export class MissingRequiredEnvVarError extends ValidationError {
constructor(envVar: EnvVar) {
super(`Missing required environment variable "${envVar.names[0]}".`);
Object.setPrototypeOf(this, MissingRequiredEnvVarError.prototype);
}
}
export class TooManyEnvVarValuesError extends CommandError {
constructor(name: string) {
super(
`An environment variable can only have one value, but "${name}" has more than one.`,
);
Object.setPrototypeOf(this, TooManyEnvVarValuesError.prototype);
}
}
export class UnexpectedOptionalEnvVarValueError extends CommandError {
constructor(name: string) {
super(
`An environment variable cannot have an optional value, but "${name}" is defined as optional.`,
);
Object.setPrototypeOf(this, UnexpectedOptionalEnvVarValueError.prototype);
}
}
export class UnexpectedVariadicEnvVarValueError extends CommandError {
constructor(name: string) {
super(
`An environment variable cannot have an variadic value, but "${name}" is defined as variadic.`,
);
Object.setPrototypeOf(this, UnexpectedVariadicEnvVarValueError.prototype);
}
}
export class DefaultCommandNotFoundError extends CommandError {
constructor(name: string, commands: Array<Command>) {
super(
`Default command "${name}" not found.${
didYouMeanCommand(name, commands)
}`,
);
Object.setPrototypeOf(this, DefaultCommandNotFoundError.prototype);
}
}
export class UnknownCompletionCommandError extends CommandError {
constructor(name: string, commands: Array<Command>) {
super(
`Auto-completion failed. Unknown command "${name}".${
didYouMeanCommand(name, commands)
}`,
);
Object.setPrototypeOf(this, UnknownCompletionCommandError.prototype);
}
}
/* Validation errors. */
export class UnknownCommandError extends ValidationError {
constructor(
name: string,
commands: Array<Command>,
excluded?: Array<string>,
) {
super(
`Unknown command "${name}".${
didYouMeanCommand(name, commands, excluded)
}`,
);
Object.setPrototypeOf(this, UnknownCommandError.prototype);
}
}
export class NoArgumentsAllowedError extends ValidationError {
constructor(name: string) {
super(`No arguments allowed for command "${name}".`);
Object.setPrototypeOf(this, NoArgumentsAllowedError.prototype);
}
}
export class MissingArgumentsError extends ValidationError {
constructor(names: Array<string>) {
super(`Missing argument(s): ${names.join(", ")}`);
Object.setPrototypeOf(this, MissingArgumentsError.prototype);
}
}
export class MissingArgumentError extends ValidationError {
constructor(name: string) {
super(`Missing argument: ${name}`);
Object.setPrototypeOf(this, MissingArgumentError.prototype);
}
}
export class TooManyArgumentsError extends ValidationError {
constructor(args: Array<string>) {
super(`Too many arguments: ${args.join(" ")}`);
Object.setPrototypeOf(this, TooManyArgumentsError.prototype);
}
}