-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(models):
ERR_UNKNOWN_FILE_EXTENSION
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
c29c652
commit aeed292
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters