generated from githuhsername/simple-template-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.ts
79 lines (74 loc) · 2.66 KB
/
error.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
import { NodeData, Options } from './core.types.js';
/** The ArgsTree error options. */
export interface ArgsTreeErrorOptions extends NodeData {
/**
* The cause error string.
* - {@linkcode ArgsTreeError.VALIDATE_ERROR}
* - {@linkcode ArgsTreeError.INVALID_OPTIONS_ERROR}
* - {@linkcode ArgsTreeError.INVALID_RANGE_ERROR}
* - {@linkcode ArgsTreeError.INVALID_SPEC_ERROR}
* - {@linkcode ArgsTreeError.UNRECOGNIZED_ALIAS_ERROR}
* - {@linkcode ArgsTreeError.UNRECOGNIZED_ARGUMENT_ERROR}
*/
cause: string;
/** The error message. */
message: string;
}
/** The ArgsTree error object. */
export interface ArgsTreeErrorObject extends ArgsTreeErrorOptions {
/** The error name. */
name: string;
}
/** The ArgsTree error. */
export class ArgsTreeError extends Error implements ArgsTreeErrorObject {
/** Validation failed from provided {@linkcode Options.validate} function. */
static readonly VALIDATE_ERROR = 'validate';
/** The {@linkcode Options} object provided is not valid (e.g. incorrect range). */
static readonly INVALID_OPTIONS_ERROR = 'invalid-options';
/** The option or command did not satisfy the required number of arguments. */
static readonly INVALID_RANGE_ERROR = 'invalid-range';
/** Failed operation for spec builder. */
static readonly INVALID_SPEC_ERROR = 'invalid-spec';
/** After an alias is parsed, it is not recognized as part of {@linkcode Options.alias}. */
static readonly UNRECOGNIZED_ALIAS_ERROR = 'unrecognized-alias';
/** The option or command is not recognized as part of {@linkcode Options.args}. */
static readonly UNRECOGNIZED_ARGUMENT_ERROR = 'unrecognized-argument';
/**
* The cause error string.
* - {@linkcode ArgsTreeError.VALIDATE_ERROR}
* - {@linkcode ArgsTreeError.INVALID_OPTIONS_ERROR}
* - {@linkcode ArgsTreeError.INVALID_RANGE_ERROR}
* - {@linkcode ArgsTreeError.INVALID_SPEC_ERROR}
* - {@linkcode ArgsTreeError.UNRECOGNIZED_ALIAS_ERROR}
* - {@linkcode ArgsTreeError.UNRECOGNIZED_ARGUMENT_ERROR}
*/
cause: string;
raw: string | null;
alias: string | null;
args: string[];
options: Options;
/**
* The ArgsTree error.
* @param options The error options.
*/
constructor(options: ArgsTreeErrorOptions) {
super(options.message, options);
this.name = this.constructor.name;
this.cause = options.cause;
this.raw = options.raw;
this.alias = options.alias;
this.args = options.args;
this.options = options.options;
}
toJSON(): ArgsTreeErrorObject {
return {
name: this.name,
cause: this.cause,
message: this.message,
raw: this.raw,
alias: this.alias,
args: this.args,
options: this.options
};
}
}