Skip to content

Commit 9b8b755

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

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @file Unit Tests - ERR_INVALID_PACKAGE_CONFIG
3+
* @module errnode/models/tests/unit/ERR_INVALID_PACKAGE_CONFIG
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import path from 'node:path'
8+
import TestSubject from '../err-invalid-package-config'
9+
10+
describe('unit:models/ERR_INVALID_PACKAGE_CONFIG', () => {
11+
let base: string
12+
let id: string
13+
let message: string
14+
let reason: string
15+
16+
beforeEach(() => {
17+
base = `bad-exports from ${import.meta.url}`
18+
id = path.resolve('node_modules/bad-exports/package.json')
19+
message = `Invalid package config ${id}`
20+
reason =
21+
'"exports" cannot contain some keys starting with \'.\' and some not. The exports object must either be an object of package subpath keys or an object of main entry condition name keys only.'
22+
})
23+
24+
it('should return Error instance', () => {
25+
// Act
26+
const result = new TestSubject(id)
27+
28+
// Expect
29+
expect(result).to.be.instanceof(Error)
30+
expect(result).to.have.property('name').equal('Error')
31+
})
32+
33+
it('should set error code', () => {
34+
expect(new TestSubject(id))
35+
.to.have.property('code')
36+
.equal(ErrorCode.ERR_INVALID_PACKAGE_CONFIG)
37+
})
38+
39+
it('should set error message', () => {
40+
// Arrange
41+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
42+
[id, undefined, undefined, message],
43+
[id, base, undefined, `${message} while importing ${base}`],
44+
[id, base, reason, `${message} while importing ${base}. ${reason}`]
45+
]
46+
47+
// Act + Expect
48+
cases.forEach(([path, base, reason, expected]) => {
49+
expect(new TestSubject(path, base, reason))
50+
.to.have.property('message')
51+
.equal(expected)
52+
})
53+
})
54+
})
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @file Error Models - ERR_INVALID_PACKAGE_CONFIG
3+
* @module errnode/models/ERR_INVALID_PACKAGE_CONFIG
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1327-L1330
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_PACKAGE_CONFIG` model.
13+
*
14+
* Thrown when a [`package.json`][1] file fails parsing.
15+
*
16+
* [1]: https://nodejs.org/api/packages.html#nodejs-packagejson-field-definitions
17+
*
18+
* @see https://nodejs.org/api/errors.html#err_invalid_package_config
19+
*
20+
* @class
21+
*
22+
* @param {string} id - Location of invalid `package.json` file
23+
* @param {string?} [base=''] - Id of module being imported. May also include
24+
* where module is being imported from
25+
* @param {string?} [reason=''] - Reason package config is invalid
26+
* @return {NodeError} `Error` instance
27+
*/
28+
const ERR_INVALID_PACKAGE_CONFIG: NodeErrorConstructor<
29+
ErrorConstructor,
30+
MessageFn<[string, string?, string?]>
31+
> = createNodeError(
32+
ErrorCode.ERR_INVALID_PACKAGE_CONFIG,
33+
Error,
34+
/**
35+
* Creates an [`ERR_INVALID_PACKAGE_CONFIG`][1] message.
36+
*
37+
* [1]: https://nodejs.org/api/errors.html#err_invalid_package_config
38+
*
39+
* @param {string} id - Location of invalid `package.json` file
40+
* @param {string?} [base=''] - Id of module being imported. May also include
41+
* where module is being imported from
42+
* @param {string?} [reason=''] - Reason package config is invalid
43+
* @return {string} Error message
44+
*/
45+
(id: string, base: string = '', reason: string = ''): string => {
46+
/**
47+
* Error message.
48+
*
49+
* @var {string} message
50+
*/
51+
let message: string = `Invalid package config ${id}`
52+
53+
// add import details
54+
if (base) message += ` while importing ${base}`
55+
56+
// add reason package config is invalid
57+
if (reason) message += `. ${reason}`
58+
59+
return message
60+
}
61+
)
62+
63+
export default ERR_INVALID_PACKAGE_CONFIG

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export { default as ERR_IMPORT_ASSERTION_TYPE_MISSING } from './err-import-asser
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'
1515
export { default as ERR_INVALID_MODULE_SPECIFIER } from './err-invalid-module-specifier'
16+
export { default as ERR_INVALID_PACKAGE_CONFIG } from './err-invalid-package-config'
1617
export { default as ERR_METHOD_NOT_IMPLEMENTED } from './err-method-not-implemented'
1718
export { default as ERR_MISSING_OPTION } from './err-missing-option'
1819
export { default as ERR_MODULE_NOT_FOUND } from './err-module-not-found'

0 commit comments

Comments
 (0)