|
| 1 | +/** |
| 2 | + * @file Error Models - ERR_INVALID_ARG_TYPE |
| 3 | + * @module errnode/models/ERR_INVALID_ARG_TYPE |
| 4 | + * @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1197-L1286 |
| 5 | + */ |
| 6 | + |
| 7 | +import { ErrorCode } from '#src/enums' |
| 8 | +import formatList from '#src/internal/format-list' |
| 9 | +import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types' |
| 10 | +import { createNodeError, determineSpecificType } from '#src/utils' |
| 11 | +import type { OneOrMany } from '@flex-development/tutils' |
| 12 | + |
| 13 | +/** |
| 14 | + * `ERR_INVALID_ARG_TYPE` model. |
| 15 | + * |
| 16 | + * Thrown when an argument of the wrong type is passed to a Node.js API. |
| 17 | + * |
| 18 | + * @see https://nodejs.org/api/errors.html#err_invalid_arg_type |
| 19 | + * |
| 20 | + * @class |
| 21 | + * |
| 22 | + * @param {string} name - Name of invalid argument or property |
| 23 | + * @param {OneOrMany<string>} expected - Expected type(s) |
| 24 | + * @param {unknown} actual - Value supplied by user |
| 25 | + * @return {NodeError<TypeError>} `TypeError` instance |
| 26 | + */ |
| 27 | +const ERR_INVALID_ARG_TYPE: NodeErrorConstructor< |
| 28 | + TypeErrorConstructor, |
| 29 | + MessageFn<[string, OneOrMany<string>, unknown]> |
| 30 | +> = createNodeError( |
| 31 | + ErrorCode.ERR_INVALID_ARG_TYPE, |
| 32 | + TypeError, |
| 33 | + /** |
| 34 | + * Creates an [`ERR_INVALID_ARG_TYPE`][1] message. |
| 35 | + * |
| 36 | + * [1]: https://nodejs.org/api/errors.html#err_invalid_arg_type |
| 37 | + * |
| 38 | + * @param {string} name - Name of invalid argument or property |
| 39 | + * @param {OneOrMany<string>} expected - Expected type(s) |
| 40 | + * @param {unknown} actual - Value supplied by user |
| 41 | + * @return {string} Error message |
| 42 | + * @throws {TypeError} If `name` is not a string, `expected` is not a string, |
| 43 | + * or `expected` is not an array containing only string values |
| 44 | + */ |
| 45 | + (name: string, expected: OneOrMany<string>, actual: unknown): string => { |
| 46 | + // ensure name is a string |
| 47 | + if (typeof name !== 'string') throw new TypeError("'name' must be a string") |
| 48 | + |
| 49 | + // ensure expected is an array |
| 50 | + if (!Array.isArray(expected)) expected = [expected] |
| 51 | + |
| 52 | + /** |
| 53 | + * Primitive value names. |
| 54 | + * |
| 55 | + * Sorted by a rough estimate on most frequently used entries. |
| 56 | + * |
| 57 | + * @const {Set<string>} k_types |
| 58 | + */ |
| 59 | + const k_types: Set<string> = new Set([ |
| 60 | + 'string', |
| 61 | + 'function', |
| 62 | + 'number', |
| 63 | + 'object', |
| 64 | + 'Function', |
| 65 | + 'Object', |
| 66 | + 'boolean', |
| 67 | + 'bigint', |
| 68 | + 'symbol' |
| 69 | + ]) |
| 70 | + |
| 71 | + /** |
| 72 | + * Error message. |
| 73 | + * |
| 74 | + * @var {string} msg |
| 75 | + */ |
| 76 | + let msg: string = 'The ' |
| 77 | + |
| 78 | + // stylize invalid argument name |
| 79 | + msg += name.endsWith(' argument') |
| 80 | + ? name |
| 81 | + : `'${name}' ${name.includes('.') ? 'property' : 'argument'}` |
| 82 | + |
| 83 | + // continue building error message |
| 84 | + msg += ' must be ' |
| 85 | + |
| 86 | + /** |
| 87 | + * Names of expected class instances. |
| 88 | + * |
| 89 | + * @const {string[]} instances |
| 90 | + */ |
| 91 | + const instances: string[] = [] |
| 92 | + |
| 93 | + /** |
| 94 | + * Names of other expected types. |
| 95 | + * |
| 96 | + * @const {string[]} other |
| 97 | + */ |
| 98 | + const other: string[] = [] |
| 99 | + |
| 100 | + /** |
| 101 | + * Names of expected primitive types. |
| 102 | + * |
| 103 | + * @const {string[]} types |
| 104 | + */ |
| 105 | + const types: string[] = [] |
| 106 | + |
| 107 | + // get expected types |
| 108 | + for (const value of expected) { |
| 109 | + if (typeof value !== 'string') { |
| 110 | + throw new TypeError('`expected` must be a string or string[]') |
| 111 | + } |
| 112 | + |
| 113 | + if (k_types.has(value)) types.push(value.toLowerCase()) |
| 114 | + else if (/^([A-Z][\da-z]*)+$/.exec(value)) instances.push(value) |
| 115 | + else other.push(value) |
| 116 | + } |
| 117 | + |
| 118 | + // special case: handle `object` in case other instances are allowed to |
| 119 | + // outline the differences between each other |
| 120 | + if (instances.length > 0) { |
| 121 | + /** |
| 122 | + * Position of `'object'` in {@linkcode types}. |
| 123 | + * |
| 124 | + * @const {number} pos |
| 125 | + */ |
| 126 | + const pos: number = types.indexOf('object') |
| 127 | + |
| 128 | + // replace 'object' in types with 'Object' in instances |
| 129 | + if (pos !== -1) { |
| 130 | + types.splice(pos, 1) |
| 131 | + instances.push('Object') |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // add expected primitive types to error message |
| 136 | + if (types.length > 0) { |
| 137 | + msg += `${types.length > 1 ? 'one ' : ''}of type` |
| 138 | + msg += ` ${formatList(types, 'or')}` |
| 139 | + if (instances.length > 0 || other.length > 0) msg += ' or ' |
| 140 | + } |
| 141 | + |
| 142 | + // add expected class instances to error message |
| 143 | + if (instances.length > 0) { |
| 144 | + msg += `an instance of ${formatList(instances, 'or')}` |
| 145 | + if (other.length > 0) msg += ' or ' |
| 146 | + } |
| 147 | + |
| 148 | + // add other expected types to error message |
| 149 | + if (other.length > 0) { |
| 150 | + if (other.length > 1) { |
| 151 | + msg += `one of ${formatList(other, 'or')}` |
| 152 | + } else { |
| 153 | + /* c8 ignore next */ |
| 154 | + if (other[0]!.toLowerCase() !== other[0]) msg += 'an ' |
| 155 | + msg += `${other[0]}` |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return `${msg}. Received ${determineSpecificType(actual)}` |
| 160 | + } |
| 161 | +) |
| 162 | + |
| 163 | +export default ERR_INVALID_ARG_TYPE |
0 commit comments