|
| 1 | +/** |
| 2 | + * @file Errors - ERR_MISSING_ARGS |
| 3 | + * @module errnode/errors/ERR_MISSING_ARGS |
| 4 | + * @see https://github.com/nodejs/node/blob/v22.8.0/lib/internal/errors.js#L1565-L1579 |
| 5 | + */ |
| 6 | + |
| 7 | +import E from '#e' |
| 8 | +import { codes } from '#src/enums' |
| 9 | +import type { NodeError, NodeErrorConstructor } from '#src/interfaces' |
| 10 | +import { formatList } from '#src/utils' |
| 11 | +import { ok } from 'devlop' |
| 12 | + |
| 13 | +/** |
| 14 | + * `ERR_MISSING_ARGS` schema. |
| 15 | + * |
| 16 | + * @see {@linkcode NodeError} |
| 17 | + * @see https://nodejs.org/api/errors.html#err_missing_args |
| 18 | + * |
| 19 | + * @extends {NodeError<codes.ERR_MISSING_ARGS>} |
| 20 | + * @extends {TypeError} |
| 21 | + */ |
| 22 | +interface ErrMissingArgs extends NodeError<codes.ERR_MISSING_ARGS>, TypeError {} |
| 23 | + |
| 24 | +/** |
| 25 | + * `ERR_MISSING_ARGS` message arguments. |
| 26 | + */ |
| 27 | +type Args = [name: string | string[], ...names: (string | string[])[]] |
| 28 | + |
| 29 | +/** |
| 30 | + * `ERR_MISSING_ARGS` constructor. |
| 31 | + * |
| 32 | + * @see {@linkcode Args} |
| 33 | + * @see {@linkcode ErrMissingArgs} |
| 34 | + * @see {@linkcode NodeErrorConstructor} |
| 35 | + * |
| 36 | + * @extends {NodeErrorConstructor<ErrMissingArgs,Args>} |
| 37 | + */ |
| 38 | +interface ErrMissingArgsConstructor |
| 39 | + extends NodeErrorConstructor<ErrMissingArgs, Args> { |
| 40 | + /** |
| 41 | + * Create a new `ERR_MISSING_ARGS` error. |
| 42 | + * |
| 43 | + * @see {@linkcode ErrMissingArgs} |
| 44 | + * |
| 45 | + * @param {string} name |
| 46 | + * Name of missing argument |
| 47 | + * @param {(string | string[])[]} names |
| 48 | + * List of missing arguments |
| 49 | + * @return {ErrMissingArgs} |
| 50 | + */ |
| 51 | + new (name: string, ...names: (string | string[])[]): ErrMissingArgs |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * `ERR_MISSING_ARGS` model. |
| 56 | + * |
| 57 | + * Thrown when a required argument of a Node.js API was not passed. |
| 58 | + * |
| 59 | + * @see {@linkcode ErrMissingArgsConstructor} |
| 60 | + * |
| 61 | + * @type {ErrMissingArgsConstructor} |
| 62 | + * |
| 63 | + * @class |
| 64 | + */ |
| 65 | +const ERR_MISSING_ARGS: ErrMissingArgsConstructor = E( |
| 66 | + codes.ERR_MISSING_ARGS, |
| 67 | + TypeError, |
| 68 | + /** |
| 69 | + * @param {(string | string[])[]} names |
| 70 | + * List of missing arguments |
| 71 | + * @return {string} |
| 72 | + * Error message |
| 73 | + */ |
| 74 | + function message(...names: (string | string[])[]): string { |
| 75 | + ok(names.length, 'At least one arg needs to be specified') |
| 76 | + |
| 77 | + /** |
| 78 | + * Wrap a string in quotes. |
| 79 | + * |
| 80 | + * @param {string} a |
| 81 | + * String to wrap |
| 82 | + * @return {string} |
| 83 | + * Wrapped string |
| 84 | + */ |
| 85 | + const wrap = (a: string): string => `"${a}"` |
| 86 | + |
| 87 | + /** |
| 88 | + * Error message. |
| 89 | + * |
| 90 | + * @var {string} message |
| 91 | + */ |
| 92 | + let message: string = 'The ' |
| 93 | + |
| 94 | + message += formatList(names.map(a => { |
| 95 | + /* c8 ignore next 2 */ return Array.isArray(a) |
| 96 | + ? a.map(wrap).join(' or ') |
| 97 | + : wrap(a) |
| 98 | + })) |
| 99 | + |
| 100 | + return `${message} argument${names.length > 1 ? 's' : ''} must be specified` |
| 101 | + } |
| 102 | +) |
| 103 | + |
| 104 | +export { |
| 105 | + ERR_MISSING_ARGS as default, |
| 106 | + type ErrMissingArgs, |
| 107 | + type Args as ErrMissingArgsArgs, |
| 108 | + type ErrMissingArgsConstructor |
| 109 | +} |
0 commit comments