Skip to content

Commit e64f15c

Browse files
committed
feat(models): ERR_INVALID_MODULE_SPECIFIER
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent aeed292 commit e64f15c

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file Unit Tests - ERR_INVALID_MODULE_SPECIFIER
3+
* @module errnode/models/tests/unit/ERR_INVALID_MODULE_SPECIFIER
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import TestSubject from '../err-invalid-module-specifier'
8+
9+
describe('unit:models/ERR_INVALID_MODULE_SPECIFIER', () => {
10+
let base: string
11+
let message: string
12+
let reason: string
13+
let request: string
14+
15+
beforeEach(() => {
16+
base = import.meta.url
17+
reason = 'empty import specifier'
18+
request = '#'
19+
message = `Invalid module '${request}'`
20+
})
21+
22+
it('should return TypeError instance', () => {
23+
expect(new TestSubject(request, reason)).to.be.instanceof(TypeError)
24+
})
25+
26+
it('should set error code', () => {
27+
expect(new TestSubject(request, reason))
28+
.to.have.property('code')
29+
.equal(ErrorCode.ERR_INVALID_MODULE_SPECIFIER)
30+
})
31+
32+
it('should set error message', () => {
33+
// Arrange
34+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
35+
[request, undefined, undefined, message],
36+
[request, reason, undefined, `${message} ${reason}`],
37+
[request, reason, base, `${message} ${reason} imported from ${base}`]
38+
]
39+
40+
// Act + Expect
41+
cases.forEach(([request, reason, base, expected]) => {
42+
expect(new TestSubject(request, reason, base))
43+
.to.have.property('message')
44+
.equal(expected)
45+
})
46+
})
47+
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @file Error Models - ERR_INVALID_MODULE_SPECIFIER
3+
* @module errnode/models/ERR_INVALID_MODULE_SPECIFIER
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1323-L1326
5+
*/
6+
7+
import { ErrorCode } from '#src/enums'
8+
import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types'
9+
import { createNodeError } from '#src/utils'
10+
11+
/**
12+
* `ERR_INVALID_MODULE_SPECIFIER` model.
13+
*
14+
* Thrown when an imported module string is an invalid URL, package name, or
15+
* package subpath specifier.
16+
*
17+
* @see https://nodejs.org/api/errors.html#err_invalid_module_specifier
18+
*
19+
* @class
20+
*
21+
* @param {string} request - Invalid module specifier
22+
* @param {string?} [reason=''] - Reason `request` is invalid
23+
* @param {string?} [base=''] - Id of module `request` was imported from
24+
* @return {NodeError<TypeError>} `TypeError` instance
25+
*/
26+
const ERR_INVALID_MODULE_SPECIFIER: NodeErrorConstructor<
27+
TypeErrorConstructor,
28+
MessageFn<[string, string?, string?]>
29+
> = createNodeError(
30+
ErrorCode.ERR_INVALID_MODULE_SPECIFIER,
31+
TypeError,
32+
/**
33+
* Creates an [`ERR_INVALID_MODULE_SPECIFIER`][1] message.
34+
*
35+
* [1]: https://nodejs.org/api/errors.html#err_invalid_module_specifier
36+
*
37+
* @param {string} request - Invalid module specifier
38+
* @param {string?} [reason=''] - Reason `request` is invalid
39+
* @param {string?} [base=''] - Id of module `request` was imported from
40+
* @return {string} Error message
41+
*/
42+
(request: string, reason: string = '', base: string = ''): string => {
43+
/**
44+
* Error message.
45+
*
46+
* @var {string} message
47+
*/
48+
let message: string = `Invalid module '${request}' ${reason}`.trimEnd()
49+
50+
// add details regarding where error occurred
51+
if (base) message += ` imported from ${base}`
52+
53+
return message
54+
}
55+
)
56+
57+
export default ERR_INVALID_MODULE_SPECIFIER

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_MODULE_SPECIFIER } from './err-invalid-module-specifier'
1516
export { default as ERR_METHOD_NOT_IMPLEMENTED } from './err-method-not-implemented'
1617
export { default as ERR_MISSING_OPTION } from './err-missing-option'
1718
export { default as ERR_NETWORK_IMPORT_DISALLOWED } from './err-network-import-disallowed'

0 commit comments

Comments
 (0)