Skip to content

Commit c86086f

Browse files
committed
feat(models): ERR_PACKAGE_IMPORT_NOT_DEFINED
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 9b8b755 commit c86086f

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @file Unit Tests - ERR_PACKAGE_IMPORT_NOT_DEFINED
3+
* @module errnode/models/tests/unit/ERR_PACKAGE_IMPORT_NOT_DEFINED
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import TestSubject from '../err-package-import-not-defined'
8+
9+
describe('unit:models/ERR_PACKAGE_IMPORT_NOT_DEFINED', () => {
10+
let base: string
11+
let dir: string
12+
let message: string
13+
let specifier: string
14+
15+
beforeEach(() => {
16+
base = import.meta.url
17+
dir = process.cwd()
18+
specifier = '#src'
19+
message = `Package import specifier '${specifier}' is not defined`
20+
})
21+
22+
it('should return TypeError instance', () => {
23+
// Act
24+
const result = new TestSubject(specifier, base)
25+
26+
// Expect
27+
expect(result).to.be.instanceof(TypeError)
28+
expect(result).to.have.property('name').equal('TypeError')
29+
})
30+
31+
it('should set error code', () => {
32+
expect(new TestSubject(specifier, base))
33+
.to.have.property('code')
34+
.equal(ErrorCode.ERR_PACKAGE_IMPORT_NOT_DEFINED)
35+
})
36+
37+
it('should set error message', () => {
38+
// Arrange
39+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
40+
[specifier, base, undefined, `${message} imported from ${base}`],
41+
[
42+
specifier,
43+
base,
44+
dir,
45+
`${message} in package ${dir}package.json imported from ${base}`
46+
]
47+
]
48+
49+
// Act + Expect
50+
cases.forEach(([specifier, base, dir, expected]) => {
51+
expect(new TestSubject(specifier, base, dir))
52+
.to.have.property('message')
53+
.equal(expected)
54+
})
55+
})
56+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @file Error Models - ERR_PACKAGE_IMPORT_NOT_DEFINED
3+
* @module errnode/models/ERR_PACKAGE_IMPORT_NOT_DEFINED
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1514-L1516
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_PACKAGE_IMPORT_NOT_DEFINED` model.
13+
*
14+
* Thrown when a `package.json` [`"imports"`][1] field does not define the given
15+
* internal package specifier mapping.
16+
*
17+
* [1]: https://nodejs.org/api/packages.html#imports
18+
*
19+
* @see https://nodejs.org/api/errors.html#err_package_import_not_defined
20+
*
21+
* @class
22+
*
23+
* @param {string} specifier - Package import specifier that is not defined
24+
* @param {string} base - Id of module `specifier` was imported from
25+
* @param {string?} [dir=''] - Directory containing `package.json` file
26+
* @return {NodeError<TypeError>} `TypeError` instance
27+
*/
28+
const ERR_PACKAGE_IMPORT_NOT_DEFINED: NodeErrorConstructor<
29+
TypeErrorConstructor,
30+
MessageFn<[string, string, string?]>
31+
> = createNodeError(
32+
ErrorCode.ERR_PACKAGE_IMPORT_NOT_DEFINED,
33+
TypeError,
34+
/**
35+
* Creates a [`ERR_PACKAGE_IMPORT_NOT_DEFINED`][1] message.
36+
*
37+
* [1]: https://nodejs.org/api/errors.html#err_package_import_not_defined
38+
*
39+
* @param {string} specifier - Package import specifier that is not defined
40+
* @param {string} base - Id of module `specifier` was imported from
41+
* @param {string?} [dir=''] - Directory containing `package.json` file
42+
* @return {string} Error message
43+
*/
44+
(specifier: string, base: string, dir: string = ''): string => {
45+
/**
46+
* Error message.
47+
*
48+
* @var {string} message
49+
*/
50+
let message: string = `Package import specifier '${specifier}' is not defined`
51+
52+
// add package.json location
53+
if (dir) message += ` in package ${dir}package.json`
54+
55+
// add import location
56+
message += ` imported from ${base}`
57+
58+
return message
59+
}
60+
)
61+
62+
export default ERR_PACKAGE_IMPORT_NOT_DEFINED

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export { default as ERR_MISSING_OPTION } from './err-missing-option'
1919
export { default as ERR_MODULE_NOT_FOUND } from './err-module-not-found'
2020
export { default as ERR_NETWORK_IMPORT_DISALLOWED } from './err-network-import-disallowed'
2121
export { default as ERR_OPERATION_FAILED } from './err-operation-failed'
22+
export { default as ERR_PACKAGE_IMPORT_NOT_DEFINED } from './err-package-import-not-defined'
2223
export { default as ERR_UNHANDLED_ERROR } from './err-unhandled-error'
2324
export { default as ERR_UNKNOWN_ENCODING } from './err-unknown-encoding'
2425
export { default as ERR_UNKNOWN_FILE_EXTENSION } from './err-unknown-file-extension'

0 commit comments

Comments
 (0)