Skip to content

Commit 6637aaa

Browse files
committed
feat(models): ERR_INVALID_ARG_VALUE
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 9a43317 commit 6637aaa

File tree

6 files changed

+161
-1
lines changed

6 files changed

+161
-1
lines changed

.eslintrc.base.cjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,10 @@ const config = {
10361036
'unicorn/error-message': 0,
10371037
'unicorn/explicit-length-check': 0,
10381038
'unicorn/no-array-for-each': 0,
1039+
'unicorn/no-useless-undefined': 0,
10391040
'unicorn/prefer-at': 0,
10401041
'unicorn/prefer-dom-node-append': 0,
1041-
'unicorn/no-useless-undefined': 0,
1042+
'unicorn/prefer-json-parse-buffer': 0,
10421043
'unicorn/string-content': 0
10431044
}
10441045
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@commitlint/types": "17.0.0",
7878
"@faker-js/faker": "7.6.0",
7979
"@flex-development/mkbuild": "1.0.0-alpha.9",
80+
"@flex-development/pkg-types": "1.0.0",
8081
"@graphql-eslint/eslint-plugin": "3.13.0",
8182
"@types/chai": "4.3.4",
8283
"@types/chai-string": "1.4.2",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file Unit Tests - ERR_INVALID_ARG_VALUE
3+
* @module errnode/models/tests/unit/ERR_INVALID_ARG_VALUE
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import type { PackageJson } from '@flex-development/pkg-types'
8+
import { inspect } from 'node-inspect-extracted'
9+
import fs from 'node:fs'
10+
import TestSubject from '../err-invalid-arg-value'
11+
12+
describe('unit:models/ERR_INVALID_ARG_VALUE', () => {
13+
let name: string
14+
let reason: string
15+
let value: unknown
16+
17+
beforeEach(() => {
18+
name = 'ip_addr'
19+
reason = `${name} should be of type string`
20+
value = null
21+
})
22+
23+
it('should return TypeError instance', () => {
24+
// Act
25+
const result = new TestSubject(name, value)
26+
27+
// Expect
28+
expect(result).to.be.instanceof(TypeError)
29+
expect(result).to.have.property('name').equal('TypeError')
30+
})
31+
32+
it('should set error code', () => {
33+
expect(new TestSubject(name, value))
34+
.to.have.property('code')
35+
.equal(ErrorCode.ERR_INVALID_ARG_VALUE)
36+
})
37+
38+
it('should set error message', () => {
39+
// Arrange
40+
const pkg: PackageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
41+
const pkgstr: string = inspect(pkg).slice(0, 128) + '...'
42+
const props = (name: string) => `props.${name}`
43+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
44+
[
45+
name,
46+
value,
47+
undefined,
48+
`The argument '${name}' is invalid. Received ${value}`
49+
],
50+
[
51+
props(name),
52+
value,
53+
undefined,
54+
`The property '${props(name)}' is invalid. Received ${value}`
55+
],
56+
[
57+
name,
58+
pkg,
59+
reason,
60+
`The argument '${name}' ${reason}. Received ${pkgstr}`
61+
]
62+
]
63+
64+
// Act + Expect
65+
cases.forEach(([name, value, reason, expected]) => {
66+
expect(new TestSubject(name, value, reason))
67+
.to.have.property('message')
68+
.equal(expected)
69+
})
70+
})
71+
})
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @file Error Models - ERR_INVALID_ARG_VALUE
3+
* @module errnode/models/ERR_INVALID_ARG_VALUE
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1287-L1294
5+
*/
6+
7+
import { ErrorCode } from '#src/enums'
8+
import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types'
9+
import { createNodeError } from '#src/utils'
10+
import { inspect } from 'node-inspect-extracted'
11+
12+
/**
13+
* `ERR_INVALID_ARG_VALUE` model.
14+
*
15+
* Thrown when an invalid or unsupported value is passed for a given argument or
16+
* property.
17+
*
18+
* @see https://nodejs.org/api/errors.html#err_invalid_arg_value
19+
*
20+
* @class
21+
*
22+
* @param {string} name - Name of invalid argument or property
23+
* @param {unknown} value - Value supplied by user
24+
* @param {string?} [reason='is invalid'] - Reason `value` is invalid
25+
* @return {NodeError<TypeError>} `TypeError` instance
26+
*/
27+
const ERR_INVALID_ARG_VALUE: NodeErrorConstructor<
28+
TypeErrorConstructor,
29+
MessageFn<[string, unknown, string?]>
30+
> = createNodeError(
31+
ErrorCode.ERR_INVALID_ARG_VALUE,
32+
TypeError,
33+
/**
34+
* Creates an [`ERR_INVALID_ARG_VALUE`][1] message.
35+
*
36+
* [1]: https://nodejs.org/api/errors.html#err_invalid_arg_value
37+
*
38+
* @param {string} name - Name of invalid argument or property
39+
* @param {unknown} value - Value supplied by user
40+
* @param {string?} [reason='is invalid'] - Reason `value` is invalid
41+
* @return {string} Error message
42+
*/
43+
(name: string, value: unknown, reason: string = 'is invalid'): string => {
44+
/**
45+
* Inspected {@linkcode value}.
46+
*
47+
* @see https://nodejs.org/api/util.html#utilinspectobject-options
48+
*
49+
* @var {string} inspected
50+
*/
51+
let inspected: string = inspect(value)
52+
53+
/**
54+
* Error message.
55+
*
56+
* @var {string} message
57+
*/
58+
let message: string = 'The'
59+
60+
// trim inspected value
61+
if (inspected.length > 128) inspected = inspected.slice(0, 128) + '...'
62+
63+
// add stylized invalid argument or property name
64+
message += ` ${name.includes('.') ? 'property' : 'argument'} '${name}'`
65+
66+
// add reason for error
67+
if (reason) message += ` ${reason}`
68+
69+
// add inspected value
70+
message += `. Received ${inspected}`
71+
72+
return message
73+
}
74+
)
75+
76+
export default ERR_INVALID_ARG_VALUE

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { default as ERR_IMPORT_ASSERTION_TYPE_FAILED } from './err-import-assert
1212
export { default as ERR_IMPORT_ASSERTION_TYPE_MISSING } from './err-import-assertion-type-missing'
1313
export { default as ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED } from './err-import-assertion-type-unsupported'
1414
export { default as ERR_INCOMPATIBLE_OPTION_PAIR } from './err-incompatible-option-pair'
15+
export { default as ERR_INVALID_ARG_VALUE } from './err-invalid-arg-value'
1516
export { default as ERR_INVALID_MODULE_SPECIFIER } from './err-invalid-module-specifier'
1617
export { default as ERR_INVALID_PACKAGE_CONFIG } from './err-invalid-package-config'
1718
export { default as ERR_METHOD_NOT_IMPLEMENTED } from './err-method-not-implemented'

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ __metadata:
999999
"@commitlint/types": "npm:17.0.0"
10001000
"@faker-js/faker": "npm:7.6.0"
10011001
"@flex-development/mkbuild": "npm:1.0.0-alpha.9"
1002+
"@flex-development/pkg-types": "npm:1.0.0"
10021003
"@flex-development/tutils": "npm:6.0.0-alpha.7"
10031004
"@graphql-eslint/eslint-plugin": "npm:3.13.0"
10041005
"@types/chai": "npm:4.3.4"
@@ -1121,6 +1122,15 @@ __metadata:
11211122
languageName: node
11221123
linkType: hard
11231124

1125+
"@flex-development/pkg-types@npm:1.0.0":
1126+
version: 1.0.0
1127+
resolution: "@flex-development/pkg-types@npm:1.0.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Fpkg-types%2F1.0.0%2F596322ff94bcaf29541e3ed76742b4777510121a"
1128+
peerDependencies:
1129+
"@flex-development/tutils": ">=6.0.0-alpha.7"
1130+
checksum: 673e66cbcb1cdfe1529014696687e625e6d57d53f435639a18fd04a50807f08588198485ff35ada7c3a8d33aeeb081d3fe4df58a5a3a8734ae74a363d308a2c6
1131+
languageName: node
1132+
linkType: hard
1133+
11241134
"@flex-development/tutils@npm:6.0.0-alpha.7":
11251135
version: 6.0.0-alpha.7
11261136
resolution: "@flex-development/tutils@npm:6.0.0-alpha.7::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ftutils%2F6.0.0-alpha.7%2F786916dcfd30bf076b5ac74cb31ca4031f74e1f0"

0 commit comments

Comments
 (0)