Skip to content

Commit 288c689

Browse files
committed
feat(models): ERR_INVALID_RETURN_VALUE
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 0dfde52 commit 288c689

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Universal API for creating [Node.js errors][1]
3232
- [`ERR_INVALID_MODULE_SPECIFIER(request[, reason][, base])`](#err_invalid_module_specifierrequest-reason-base)
3333
- [`ERR_INVALID_PACKAGE_CONFIG(id[, base][, reason])`](#err_invalid_package_configid-base-reason)
3434
- [`ERR_INVALID_PACKAGE_TARGET(dir, key, target[, internal][, base])`](#err_invalid_package_targetdir-key-target-internal-base)
35+
- [`ERR_INVALID_RETURN_VALUE(expected, name, value)`](#err_invalid_return_valueexpected-name-value)
3536
- [`ERR_INVALID_URL(input)`](#err_invalid_urlinput)
3637
- [`ERR_METHOD_NOT_IMPLEMENTED(method)`](#err_method_not_implementedmethod)
3738
- [`ERR_MISSING_OPTION(option)`](#err_missing_optionoption)
@@ -218,6 +219,7 @@ This package exports the following identifiers:
218219
- [`ERR_INVALID_MODULE_SPECIFIER`](#err_invalid_module_specifierrequest-reason-base)
219220
- [`ERR_INVALID_PACKAGE_CONFIG`](#err_invalid_package_configid-base-reason)
220221
- [`ERR_INVALID_PACKAGE_TARGET`](#err_invalid_package_targetdir-key-target-internal-base)
222+
- [`ERR_INVALID_RETURN_VALUE`](#err_invalid_return_valueexpected-name-value)
221223
- [`ERR_INVALID_URL`](#err_invalid_urlinput)
222224
- [`ERR_METHOD_NOT_IMPLEMENTED`](#err_method_not_implementedmethod)
223225
- [`ERR_MISSING_OPTION`](#err_missing_optionoption)
@@ -407,6 +409,18 @@ the attempted module resolution.
407409

408410
> **Source**: [`src/models/err-invalid-package-target.ts`](src/models/err-invalid-package-target.ts)
409411
412+
#### `ERR_INVALID_RETURN_VALUE(expected, name, value)`
413+
414+
Thrown when a function does not return an expected value type on execution, such as when a function is expected to
415+
return a promise.
416+
417+
- `{string}` **`expected`** &mdash; Expected return value type
418+
- `{string}` **`name`** &mdash; Name of function that returned invalid value type
419+
- `{unknown}` **`value`** &mdash; Value supplied by user
420+
- **Returns**: `{NodeError<TypeError>}`
421+
422+
> **Source**: [`src/models/err-invalid-return-value.ts`](src/models/err-invalid-return-value.ts)
423+
410424
#### `ERR_INVALID_URL(input)`
411425

412426
Thrown when an invalid URL is passed to a [WHATWG][11] [`URL` constructor][12] or [`url.parse()`][13] to be parsed.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file Unit Tests - ERR_INVALID_RETURN_VALUE
3+
* @module errnode/models/tests/unit/ERR_INVALID_RETURN_VALUE
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import type { NodeError } from '#src/types'
8+
import { determineSpecificType } from '#src/utils'
9+
import TestSubject from '../err-invalid-return-value'
10+
11+
describe('unit:models/ERR_INVALID_RETURN_VALUE', () => {
12+
let expected: string
13+
let name: string
14+
let result: NodeError<TypeError>
15+
let value: unknown
16+
17+
beforeEach(() => {
18+
expected = 'string'
19+
name = 'fn'
20+
value = null
21+
result = new TestSubject(expected, name, value)
22+
})
23+
24+
it('should return TypeError instance', () => {
25+
expect(result).to.be.instanceof(TypeError)
26+
expect(result).to.have.property('name').equal('TypeError')
27+
})
28+
29+
it('should set error code', () => {
30+
expect(result)
31+
.to.have.property('code')
32+
.equal(ErrorCode.ERR_INVALID_RETURN_VALUE)
33+
})
34+
35+
it('should set error message', () => {
36+
// Arrange
37+
const type: string = determineSpecificType(value)
38+
const msg: string = `Expected ${expected} to be returned from the '${name}' function but got ${type}.`
39+
40+
// Expect
41+
expect(result).to.have.property('message').equal(msg)
42+
})
43+
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file Error Models - ERR_INVALID_RETURN_VALUE
3+
* @module errnode/models/ERR_INVALID_RETURN_VALUE
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1367-L1372
5+
*/
6+
7+
import { ErrorCode } from '#src/enums'
8+
import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types'
9+
import { createNodeError, determineSpecificType } from '#src/utils'
10+
11+
/**
12+
* `ERR_INVALID_RETURN_VALUE` model.
13+
*
14+
* Thrown when a function does not return an expected value type on execution,
15+
* such as when a function is expected to return a promise.
16+
*
17+
* @see https://nodejs.org/api/errors.html#err_invalid_return_value
18+
*
19+
* @class
20+
*
21+
* @param {string} expected - Expected return value type
22+
* @param {string} name - Name of function that returned invalid value type
23+
* @param {unknown} value - Value supplied by user
24+
* @return {NodeError<TypeError>} `TypeError` instance
25+
*/
26+
const ERR_INVALID_RETURN_VALUE: NodeErrorConstructor<
27+
TypeErrorConstructor,
28+
MessageFn<[string, string, unknown]>
29+
> = createNodeError(
30+
ErrorCode.ERR_INVALID_RETURN_VALUE,
31+
TypeError,
32+
/**
33+
* Creates a [`ERR_INVALID_RETURN_VALUE`][1] message.
34+
*
35+
* [1]: https://nodejs.org/api/errors.html#err_invalid_return_value
36+
*
37+
* @param {string} expected - Expected return value type
38+
* @param {string} name - Name of function that returned invalid value type
39+
* @param {unknown} value - Value supplied by user
40+
* @return {string} Error message
41+
*/
42+
(expected: string, name: string, value: unknown): string => {
43+
// prettier-ignore
44+
return `Expected ${expected} to be returned from the '${name}' function but got ${determineSpecificType(value)}.`
45+
}
46+
)
47+
48+
export default ERR_INVALID_RETURN_VALUE

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export { default as ERR_INVALID_ARG_VALUE } from './err-invalid-arg-value'
1717
export { default as ERR_INVALID_MODULE_SPECIFIER } from './err-invalid-module-specifier'
1818
export { default as ERR_INVALID_PACKAGE_CONFIG } from './err-invalid-package-config'
1919
export { default as ERR_INVALID_PACKAGE_TARGET } from './err-invalid-package-target'
20+
export { default as ERR_INVALID_RETURN_VALUE } from './err-invalid-return-value'
2021
export { default as ERR_INVALID_URL } from './err-invalid-url'
2122
export { default as ERR_METHOD_NOT_IMPLEMENTED } from './err-method-not-implemented'
2223
export { default as ERR_MISSING_OPTION } from './err-missing-option'

0 commit comments

Comments
 (0)