Skip to content

Commit

Permalink
feat(models): ERR_UNKNOWN_FILE_EXTENSION
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Dec 31, 2022
1 parent c29c652 commit aeed292
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/models/__tests__/err-unknown-file-extension.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @file Unit Tests - ERR_UNKNOWN_FILE_EXTENSION
* @module errnode/models/tests/unit/ERR_UNKNOWN_FILE_EXTENSION
*/

import { ErrorCode } from '#src/enums'
import path from 'node:path'
import TestSubject from '../err-unknown-file-extension'

describe('unit:models/ERR_UNKNOWN_FILE_EXTENSION', () => {
let ext: string
let id: string
let message: string
let suggestion: string

beforeEach(() => {
id = import.meta.url
ext = path.extname(id)
message = `Unknown file extension '${ext}' for ${id}`
suggestion = 'Use a custom loader'
})

it('should return TypeError instance', () => {
// Act
const result = new TestSubject(ext, id)

// Expect
expect(result).to.have.property('name').equal('TypeError')
expect(result).to.be.instanceof(TypeError)
})

it('should set error code', () => {
expect(new TestSubject(ext, id))
.to.have.property('code')
.equal(ErrorCode.ERR_UNKNOWN_FILE_EXTENSION)
})

it('should set error message', () => {
// Arrange
const cases: [...ConstructorParameters<typeof TestSubject>, string][] = [
[ext, id, undefined, message],
[ext, id, suggestion, `${message}. ${suggestion}`]
]

// Act + Expect
cases.forEach(([ext, id, suggestion, expected]) => {
expect(new TestSubject(ext, id, suggestion))
.to.have.property('message')
.equal(expected)
})
})
})
57 changes: 57 additions & 0 deletions src/models/err-unknown-file-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file Error Models - ERR_UNKNOWN_FILE_EXTENSION
* @module errnode/models/ERR_UNKNOWN_FILE_EXTENSION
* @see https://github.com/nodejs/node/blob/v19.3.0/lib/internal/errors.js#L1686-L1692
*/

import { ErrorCode } from '#src/enums'
import type { MessageFn, NodeError, NodeErrorConstructor } from '#src/types'
import { createNodeError } from '#src/utils'

/**
* `ERR_UNKNOWN_FILE_EXTENSION` model.
*
* Thrown when an attempt is made to load a module with an unknown or
* unsupported file extension.
*
* @see https://nodejs.org/api/errors.html#err_unknown_file_extension
*
* @class
*
* @param {string} ext - Unknown or unsupported file extension
* @param {string} id - Id of module containing `ext`
* @param {string?} [suggestion=''] - Recommended fix
* @return {NodeError<TypeError>} `TypeError` instance
*/
const ERR_UNKNOWN_FILE_EXTENSION: NodeErrorConstructor<
TypeErrorConstructor,
MessageFn<[string, string, string?]>
> = createNodeError(
ErrorCode.ERR_UNKNOWN_FILE_EXTENSION,
TypeError,
/**
* Creates an [`ERR_UNKNOWN_FILE_EXTENSION`][1] message.
*
* [1]: https://nodejs.org/api/errors.html#err_unknown_file_extension
*
* @param {string} ext - Unknown or unsupported file extension
* @param {string} id - Id of module containing `ext`
* @param {string?} [suggestion=''] - Recommended fix
* @return {string} Error message
*/
(ext: string, id: string, suggestion: string = ''): string => {
/**
* Error message.
*
* @var {string} message
*/
let message: string = `Unknown file extension '${ext}' for ${id}`

// add recommended fix
if (suggestion) message += `. ${suggestion}`

return message
}
)

export default ERR_UNKNOWN_FILE_EXTENSION
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export { default as ERR_MISSING_OPTION } from './err-missing-option'
export { default as ERR_NETWORK_IMPORT_DISALLOWED } from './err-network-import-disallowed'
export { default as ERR_OPERATION_FAILED } from './err-operation-failed'
export { default as ERR_UNKNOWN_ENCODING } from './err-unknown-encoding'
export { default as ERR_UNKNOWN_FILE_EXTENSION } from './err-unknown-file-extension'
export { default as ERR_UNKNOWN_MODULE_FORMAT } from './err-unknown-module-format'
export { default as ERR_UNSUPPORTED_DIR_IMPORT } from './err-unsupported-dir-import'

0 comments on commit aeed292

Please sign in to comment.