-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr-missing-args.ts
109 lines (99 loc) · 2.59 KB
/
err-missing-args.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
/**
* @file Errors - ERR_MISSING_ARGS
* @module errnode/errors/ERR_MISSING_ARGS
* @see https://github.com/nodejs/node/blob/v22.8.0/lib/internal/errors.js#L1565-L1579
*/
import E from '#e'
import { codes } from '#src/enums'
import type { NodeError, NodeErrorConstructor } from '#src/interfaces'
import { formatList } from '#src/utils'
import { ok } from 'devlop'
/**
* `ERR_MISSING_ARGS` schema.
*
* @see {@linkcode NodeError}
* @see https://nodejs.org/api/errors.html#err_missing_args
*
* @extends {NodeError<codes.ERR_MISSING_ARGS>}
* @extends {TypeError}
*/
interface ErrMissingArgs extends NodeError<codes.ERR_MISSING_ARGS>, TypeError {}
/**
* `ERR_MISSING_ARGS` message arguments.
*/
type Args = [name: string | string[], ...names: (string | string[])[]]
/**
* `ERR_MISSING_ARGS` constructor.
*
* @see {@linkcode Args}
* @see {@linkcode ErrMissingArgs}
* @see {@linkcode NodeErrorConstructor}
*
* @extends {NodeErrorConstructor<ErrMissingArgs,Args>}
*/
interface ErrMissingArgsConstructor
extends NodeErrorConstructor<ErrMissingArgs, Args> {
/**
* Create a new `ERR_MISSING_ARGS` error.
*
* @see {@linkcode ErrMissingArgs}
*
* @param {string} name
* Name of missing argument
* @param {(string | string[])[]} names
* List of missing arguments
* @return {ErrMissingArgs}
*/
new (name: string, ...names: (string | string[])[]): ErrMissingArgs
}
/**
* `ERR_MISSING_ARGS` model.
*
* Thrown when a required argument of a Node.js API was not passed.
*
* @see {@linkcode ErrMissingArgsConstructor}
*
* @type {ErrMissingArgsConstructor}
*
* @class
*/
const ERR_MISSING_ARGS: ErrMissingArgsConstructor = E(
codes.ERR_MISSING_ARGS,
TypeError,
/**
* @param {(string | string[])[]} names
* List of missing arguments
* @return {string}
* Error message
*/
function message(...names: (string | string[])[]): string {
ok(names.length, 'At least one arg needs to be specified')
/**
* Wrap a string in quotes.
*
* @param {string} a
* String to wrap
* @return {string}
* Wrapped string
*/
const wrap = (a: string): string => `"${a}"`
/**
* Error message.
*
* @var {string} message
*/
let message: string = 'The '
message += formatList(names.map(a => {
/* c8 ignore next 2 */ return Array.isArray(a)
? a.map(wrap).join(' or ')
: wrap(a)
}))
return `${message} argument${names.length > 1 ? 's' : ''} must be specified`
}
)
export {
ERR_MISSING_ARGS as default,
type ErrMissingArgs,
type Args as ErrMissingArgsArgs,
type ErrMissingArgsConstructor
}