Skip to content

Commit 028aa6b

Browse files
committed
feat(models): ERR_MODULE_NOT_FOUND
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 61ed62a commit 028aa6b

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @file Unit Tests - ERR_MODULE_NOT_FOUND
3+
* @module errnode/models/tests/unit/ERR_MODULE_NOT_FOUND
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import TestSubject from '../err-module-not-found'
8+
9+
describe('unit:models/ERR_MODULE_NOT_FOUND', () => {
10+
let base: string
11+
let id: string
12+
13+
beforeEach(() => {
14+
base = import.meta.url
15+
id = 'target'
16+
})
17+
18+
it('should return Error instance', () => {
19+
expect(new TestSubject(id, base)).to.be.instanceof(Error)
20+
})
21+
22+
it('should set error code', () => {
23+
expect(new TestSubject(id, base))
24+
.to.have.property('code')
25+
.equal(ErrorCode.ERR_MODULE_NOT_FOUND)
26+
})
27+
28+
it('should set error message', () => {
29+
// Arrange
30+
const cases: ConstructorParameters<typeof TestSubject>[] = [
31+
[id, base],
32+
[id, base, 'module']
33+
]
34+
35+
// Act + Expect
36+
cases.forEach(([id, base, type]) => {
37+
expect(new TestSubject(id, base, type))
38+
.to.have.property('message')
39+
.equal(`Cannot find ${type ?? 'package'} '${id}' imported from ${base}`)
40+
})
41+
})
42+
})

src/models/err-module-not-found.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @file Error Models - ERR_MODULE_NOT_FOUND
3+
* @module errnode/models/ERR_MODULE_NOT_FOUND
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1470-L1472
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_MODULE_NOT_FOUND` model.
13+
*
14+
* Thrown when a module file cannot be resolved by the ECMAScript modules loader
15+
* while attempting an `import` operation or when loading a program entry point.
16+
*
17+
* @see https://nodejs.org/api/errors.html#err_module_not_found
18+
*
19+
* @class
20+
*
21+
* @param {string} id - Id of missing module
22+
* @param {string} base - Id of module `id` was imported from
23+
* @param {string?} [type='package'] - Module file type
24+
* @return {NodeError} `Error` instance
25+
*/
26+
const ERR_MODULE_NOT_FOUND: NodeErrorConstructor<
27+
ErrorConstructor,
28+
MessageFn<[string, string, string?]>
29+
> = createNodeError(
30+
ErrorCode.ERR_MODULE_NOT_FOUND,
31+
Error,
32+
/**
33+
* Creates an [`ERR_MODULE_NOT_FOUND`][1] message.
34+
*
35+
* [1]: https://nodejs.org/api/errors.html#err_module_not_found
36+
*
37+
* @param {string} id - Id of missing module
38+
* @param {string} base - Id of module `id` was imported from
39+
* @param {string?} [type='package'] - Module file type
40+
* @return {string} Error message
41+
*/
42+
(id: string, base: string, type: string = 'package'): string => {
43+
return `Cannot find ${type} '${id}' imported from ${base}`
44+
}
45+
)
46+
47+
export default ERR_MODULE_NOT_FOUND

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export { default as ERR_INCOMPATIBLE_OPTION_PAIR } from './err-incompatible-opti
1515
export { default as ERR_INVALID_MODULE_SPECIFIER } from './err-invalid-module-specifier'
1616
export { default as ERR_METHOD_NOT_IMPLEMENTED } from './err-method-not-implemented'
1717
export { default as ERR_MISSING_OPTION } from './err-missing-option'
18+
export { default as ERR_MODULE_NOT_FOUND } from './err-module-not-found'
1819
export { default as ERR_NETWORK_IMPORT_DISALLOWED } from './err-network-import-disallowed'
1920
export { default as ERR_OPERATION_FAILED } from './err-operation-failed'
2021
export { default as ERR_UNHANDLED_ERROR } from './err-unhandled-error'

0 commit comments

Comments
 (0)