Skip to content

Commit aeed292

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

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file Unit Tests - ERR_UNKNOWN_FILE_EXTENSION
3+
* @module errnode/models/tests/unit/ERR_UNKNOWN_FILE_EXTENSION
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import path from 'node:path'
8+
import TestSubject from '../err-unknown-file-extension'
9+
10+
describe('unit:models/ERR_UNKNOWN_FILE_EXTENSION', () => {
11+
let ext: string
12+
let id: string
13+
let message: string
14+
let suggestion: string
15+
16+
beforeEach(() => {
17+
id = import.meta.url
18+
ext = path.extname(id)
19+
message = `Unknown file extension '${ext}' for ${id}`
20+
suggestion = 'Use a custom loader'
21+
})
22+
23+
it('should return TypeError instance', () => {
24+
// Act
25+
const result = new TestSubject(ext, id)
26+
27+
// Expect
28+
expect(result).to.have.property('name').equal('TypeError')
29+
expect(result).to.be.instanceof(TypeError)
30+
})
31+
32+
it('should set error code', () => {
33+
expect(new TestSubject(ext, id))
34+
.to.have.property('code')
35+
.equal(ErrorCode.ERR_UNKNOWN_FILE_EXTENSION)
36+
})
37+
38+
it('should set error message', () => {
39+
// Arrange
40+
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
41+
[ext, id, undefined, message],
42+
[ext, id, suggestion, `${message}. ${suggestion}`]
43+
]
44+
45+
// Act + Expect
46+
cases.forEach(([ext, id, suggestion, expected]) => {
47+
expect(new TestSubject(ext, id, suggestion))
48+
.to.have.property('message')
49+
.equal(expected)
50+
})
51+
})
52+
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @file Error Models - ERR_UNKNOWN_FILE_EXTENSION
3+
* @module errnode/models/ERR_UNKNOWN_FILE_EXTENSION
4+
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1686-L1692
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_UNKNOWN_FILE_EXTENSION` model.
13+
*
14+
* Thrown when an attempt is made to load a module with an unknown or
15+
* unsupported file extension.
16+
*
17+
* @see https://nodejs.org/api/errors.html#err_unknown_file_extension
18+
*
19+
* @class
20+
*
21+
* @param {string} ext - Unknown or unsupported file extension
22+
* @param {string} id - Id of module containing `ext`
23+
* @param {string?} [suggestion=''] - Recommended fix
24+
* @return {NodeError<TypeError>} `TypeError` instance
25+
*/
26+
const ERR_UNKNOWN_FILE_EXTENSION: NodeErrorConstructor<
27+
TypeErrorConstructor,
28+
MessageFn<[string, string, string?]>
29+
> = createNodeError(
30+
ErrorCode.ERR_UNKNOWN_FILE_EXTENSION,
31+
TypeError,
32+
/**
33+
* Creates an [`ERR_UNKNOWN_FILE_EXTENSION`][1] message.
34+
*
35+
* [1]: https://nodejs.org/api/errors.html#err_unknown_file_extension
36+
*
37+
* @param {string} ext - Unknown or unsupported file extension
38+
* @param {string} id - Id of module containing `ext`
39+
* @param {string?} [suggestion=''] - Recommended fix
40+
* @return {string} Error message
41+
*/
42+
(ext: string, id: string, suggestion: string = ''): string => {
43+
/**
44+
* Error message.
45+
*
46+
* @var {string} message
47+
*/
48+
let message: string = `Unknown file extension '${ext}' for ${id}`
49+
50+
// add recommended fix
51+
if (suggestion) message += `. ${suggestion}`
52+
53+
return message
54+
}
55+
)
56+
57+
export default ERR_UNKNOWN_FILE_EXTENSION

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export { default as ERR_MISSING_OPTION } from './err-missing-option'
1717
export { default as ERR_NETWORK_IMPORT_DISALLOWED } from './err-network-import-disallowed'
1818
export { default as ERR_OPERATION_FAILED } from './err-operation-failed'
1919
export { default as ERR_UNKNOWN_ENCODING } from './err-unknown-encoding'
20+
export { default as ERR_UNKNOWN_FILE_EXTENSION } from './err-unknown-file-extension'
2021
export { default as ERR_UNKNOWN_MODULE_FORMAT } from './err-unknown-module-format'
2122
export { default as ERR_UNSUPPORTED_DIR_IMPORT } from './err-unsupported-dir-import'

0 commit comments

Comments
 (0)