Skip to content

Commit f744782

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

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file Unit Tests - ERR_PACKAGE_PATH_NOT_EXPORTED
3+
* @module errnode/models/tests/unit/ERR_PACKAGE_PATH_NOT_EXPORTED
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import TestSubject from '../err-package-path-not-exported'
8+
9+
describe('unit:models/ERR_PACKAGE_PATH_NOT_EXPORTED', () => {
10+
let base: string
11+
let dir: string
12+
let subpath: string
13+
14+
beforeEach(() => {
15+
base = import.meta.url
16+
dir = process.cwd() + 'node_modules/@flex-development/errnode/'
17+
subpath = '.'
18+
})
19+
20+
it('should return Error instance', () => {
21+
// Act
22+
const result = new TestSubject(dir, subpath)
23+
24+
// Expect
25+
expect(result).to.be.instanceof(Error)
26+
expect(result).to.have.property('name').equal('Error')
27+
})
28+
29+
it('should set error code', () => {
30+
expect(new TestSubject(dir, subpath))
31+
.to.have.property('code')
32+
.equal(ErrorCode.ERR_PACKAGE_PATH_NOT_EXPORTED)
33+
})
34+
35+
it('should set error message', () => {
36+
// Arrange
37+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
38+
[
39+
dir,
40+
subpath,
41+
undefined,
42+
`No 'exports' main defined in ${dir}package.json`
43+
],
44+
[
45+
dir,
46+
subpath,
47+
base,
48+
`No 'exports' main defined in ${dir}package.json imported from ${base}`
49+
],
50+
[
51+
dir,
52+
subpath + '/internal/format-message',
53+
undefined,
54+
`Package subpath '${subpath}/internal/format-message' is not defined by 'exports' in ${dir}package.json`
55+
],
56+
[
57+
dir,
58+
subpath + '/utils/create-node-error',
59+
base,
60+
`Package subpath '${subpath}/utils/create-node-error' is not defined by 'exports' in ${dir}package.json imported from ${base}`
61+
]
62+
]
63+
64+
// Act + Expect
65+
cases.forEach(([dir, subpath, base, expected]) => {
66+
expect(new TestSubject(dir, subpath, base))
67+
.to.have.property('message')
68+
.equal(expected)
69+
})
70+
})
71+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @file Error Models - ERR_PACKAGE_PATH_NOT_EXPORTED
3+
* @module errnode/models/ERR_PACKAGE_PATH_NOT_EXPORTED
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1518-L1524
5+
*/
6+
7+
import { ErrorCode } from '#src/enums'
8+
import type { NodeError, NodeErrorConstructor } from '#src/types'
9+
import { createNodeError } from '#src/utils'
10+
11+
/**
12+
* `ERR_PACKAGE_PATH_NOT_EXPORTED` model.
13+
*
14+
* Thrown when a `package.json` [`"exports"`][1] field does not export a
15+
* requested subpath.
16+
*
17+
* [1]: https://nodejs.org/api/packages.html#exports
18+
*
19+
* @see https://nodejs.org/api/errors.html#err_package_path_not_exported
20+
*
21+
* @class
22+
*
23+
* @param {string} dir - Directory containing `package.json` file
24+
* @param {string} subpath - Invalid subpath
25+
* @param {string?} [base=''] - Id of module `subpath` was imported from
26+
* @return {NodeError} `Error` instance
27+
*/
28+
const ERR_PACKAGE_PATH_NOT_EXPORTED: NodeErrorConstructor<
29+
ErrorConstructor,
30+
[string, string, string?]
31+
> = createNodeError(
32+
ErrorCode.ERR_PACKAGE_PATH_NOT_EXPORTED,
33+
Error,
34+
/**
35+
* Creates an [`ERR_PACKAGE_PATH_NOT_EXPORTED`][1] message.
36+
*
37+
* [1]: https://nodejs.org/api/errors.html#err_package_path_not_exported
38+
*
39+
* @param {string} dir - Directory containing `package.json` file
40+
* @param {string} subpath - Invalid subpath
41+
* @param {string?} [base=''] - Id of module `subpath` was imported from
42+
* @return {string} Error message
43+
*/
44+
(dir: string, subpath: string, base: string = ''): string => {
45+
/**
46+
* Error message.
47+
*
48+
* @var {string} message
49+
*/
50+
let message: string =
51+
subpath === '.'
52+
? "No 'exports' main defined in"
53+
: `Package subpath '${subpath}' is not defined by 'exports' in`
54+
55+
// add package.json location
56+
message += ` ${dir}package.json`
57+
58+
// add import location
59+
if (base) message += ` imported from ${base}`
60+
61+
return message
62+
}
63+
)
64+
65+
export default ERR_PACKAGE_PATH_NOT_EXPORTED

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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'
2222
export { default as ERR_PACKAGE_IMPORT_NOT_DEFINED } from './err-package-import-not-defined'
23+
export { default as ERR_PACKAGE_PATH_NOT_EXPORTED } from './err-package-path-not-exported'
2324
export { default as ERR_UNHANDLED_ERROR } from './err-unhandled-error'
2425
export { default as ERR_UNKNOWN_ENCODING } from './err-unknown-encoding'
2526
export { default as ERR_UNKNOWN_FILE_EXTENSION } from './err-unknown-file-extension'

0 commit comments

Comments
 (0)