From c6305f85b5542f407129915602f10eb45dc5837a Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Sun, 25 Aug 2024 20:53:42 -0400 Subject: [PATCH] feat(errors): `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` Signed-off-by: Lexus Drumgold --- README.md | 1 + __fixtures__/js-module-data-url.ts | 13 ++++ src/__snapshots__/errors.integration.snap | 2 + src/__snapshots__/index.e2e.snap | 1 + src/__tests__/errors.integration.spec.ts | 7 ++ ...port-attribute-type-incompatible.spec-d.ts | 48 ++++++++++++ .../err-import-attribute-type-incompatible.ts | 77 +++++++++++++++++++ src/errors/index.ts | 6 ++ 8 files changed, 155 insertions(+) create mode 100644 __fixtures__/js-module-data-url.ts create mode 100644 src/errors/__tests__/err-import-attribute-type-incompatible.spec-d.ts create mode 100644 src/errors/err-import-attribute-type-incompatible.ts diff --git a/README.md b/README.md index 71066e9c..65001bc6 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ This package exports the following identifiers: - `ERR_IMPORT_ASSERTION_TYPE_MISSING` - `ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED` - `ERR_IMPORT_ATTRIBUTE_MISSING` + - `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` - `ERR_INCOMPATIBLE_OPTION_PAIR` - `ERR_INVALID_ARG_TYPE` - `ERR_INVALID_ARG_VALUE` diff --git a/__fixtures__/js-module-data-url.ts b/__fixtures__/js-module-data-url.ts new file mode 100644 index 00000000..248f4de3 --- /dev/null +++ b/__fixtures__/js-module-data-url.ts @@ -0,0 +1,13 @@ +/** + * @file Fixtures - JS_MODULE_DATA_URL + * @module fixtures/JS_MODULE_DATA_URL + */ + +/** + * JavaScript module `data:` URL. + * + * @const {string} JS_MODULE_DATA_URL + */ +const JS_MODULE_DATA_URL: string = 'data:text/javascript,export{}' + +export default JS_MODULE_DATA_URL diff --git a/src/__snapshots__/errors.integration.snap b/src/__snapshots__/errors.integration.snap index 7b76304d..7224e8af 100644 --- a/src/__snapshots__/errors.integration.snap +++ b/src/__snapshots__/errors.integration.snap @@ -10,6 +10,8 @@ exports[`integration:errors > ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED > #toString exports[`integration:errors > ERR_IMPORT_ATTRIBUTE_MISSING > #toString > should return string representation of error 1`] = `TypeError [ERR_IMPORT_ATTRIBUTE_MISSING]: Module "data:application/json,""" needs an import attribute of "type: json"`; +exports[`integration:errors > ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE > #toString > should return string representation of error 1`] = `TypeError [ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE]: Module "data:text/javascript,export{}" is not of type "json"`; + exports[`integration:errors > ERR_INCOMPATIBLE_OPTION_PAIR > #toString > should return string representation of error 1`] = `TypeError [ERR_INCOMPATIBLE_OPTION_PAIR]: Option 'N' cannot be used in combination with option 'cost'`; exports[`integration:errors > ERR_INVALID_ARG_TYPE > #toString > should return string representation of error 1`] = `TypeError [ERR_INVALID_ARG_TYPE]: The 'ctor' argument must be of type function. Received null`; diff --git a/src/__snapshots__/index.e2e.snap b/src/__snapshots__/index.e2e.snap index 4d440da8..0a9ec16d 100644 --- a/src/__snapshots__/index.e2e.snap +++ b/src/__snapshots__/index.e2e.snap @@ -10,6 +10,7 @@ exports[`e2e:errnode > should expose public api 1`] = ` "ERR_IMPORT_ASSERTION_TYPE_MISSING", "ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED", "ERR_IMPORT_ATTRIBUTE_MISSING", + "ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE", "ERR_INCOMPATIBLE_OPTION_PAIR", "ERR_INVALID_ARG_TYPE", "ERR_INVALID_ARG_VALUE", diff --git a/src/__tests__/errors.integration.spec.ts b/src/__tests__/errors.integration.spec.ts index 21e254a8..4fcfd691 100644 --- a/src/__tests__/errors.integration.spec.ts +++ b/src/__tests__/errors.integration.spec.ts @@ -3,6 +3,7 @@ * @module errnode/tests/integration/errors */ +import JS_MODULE_DATA_URL from '#fixtures/js-module-data-url' import JSON_MODULE_DATA_URL from '#fixtures/json-module-data-url' import { codes, syscodes } from '#src/enums' import type { NodeError, NodeErrorConstructor } from '#src/interfaces' @@ -41,6 +42,12 @@ describe('integration:errors', () => { 'type', 'json' ], + [ + codes.ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE, + TypeError, + JS_MODULE_DATA_URL, + 'json' + ], [codes.ERR_INCOMPATIBLE_OPTION_PAIR, TypeError, 'N', 'cost'], [codes.ERR_INVALID_ARG_TYPE, TypeError, 'ctor', 'Function', null], [codes.ERR_INVALID_ARG_VALUE, TypeError, 'address', 1], diff --git a/src/errors/__tests__/err-import-attribute-type-incompatible.spec-d.ts b/src/errors/__tests__/err-import-attribute-type-incompatible.spec-d.ts new file mode 100644 index 00000000..0c283da2 --- /dev/null +++ b/src/errors/__tests__/err-import-attribute-type-incompatible.spec-d.ts @@ -0,0 +1,48 @@ +/** + * @file Type Tests - ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE + * @module errnode/errors/tests/unit-d/ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE + */ + +import { codes } from '#src/enums' +import type { NodeError, NodeErrorConstructor } from '#src/interfaces' +import type * as TestSubject from '../err-import-attribute-type-incompatible' + +describe('unit-d:errors/ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', () => { + describe('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', () => { + it('should be ErrImportAttributeTypeIncompatibleConstructor', () => { + // Arrange + type Expect = TestSubject.ErrImportAttributeTypeIncompatibleConstructor + + // Expect + expectTypeOf().toEqualTypeOf() + }) + }) + + describe('ErrImportAttributeTypeIncompatible', () => { + it('should extend NodeError', () => { + // Arrange + type Expect = NodeError + + // Expect + expectTypeOf() + .toMatchTypeOf() + }) + + it('should extend TypeError', () => { + expectTypeOf() + .toMatchTypeOf() + }) + }) + + describe('ErrImportAttributeTypeIncompatibleConstructor', () => { + it('should match NodeErrorConstructor', () => { + // Arrange + type T = TestSubject.ErrImportAttributeTypeIncompatible + type Args = TestSubject.ErrImportAttributeTypeIncompatibleArgs + + // Expect + expectTypeOf() + .toMatchTypeOf>() + }) + }) +}) diff --git a/src/errors/err-import-attribute-type-incompatible.ts b/src/errors/err-import-attribute-type-incompatible.ts new file mode 100644 index 00000000..e61321c9 --- /dev/null +++ b/src/errors/err-import-attribute-type-incompatible.ts @@ -0,0 +1,77 @@ +/** + * @file Errors - ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE + * @module errnode/errors/ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE + * @see https://github.com/nodejs/node/blob/v22.7.0/lib/internal/errors.js#L1342-L1343 + */ + +import E from '#e' +import { codes } from '#src/enums' +import type { NodeError, NodeErrorConstructor } from '#src/interfaces' + +/** + * `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` schema. + * + * @see {@linkcode NodeError} + * @see https://nodejs.org/api/errors.html#err_import_attribute_type_incompatible + * + * @extends {NodeError} + * @extends {TypeError} + */ +interface ErrImportAttributeTypeIncompatible + extends NodeError, TypeError {} + +/** + * `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` message arguments. + */ +type Args = [id: string, type: string] + +/** + * `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` constructor. + * + * @see {@linkcode Args} + * @see {@linkcode ErrImportAttributeTypeIncompatible} + * @see {@linkcode NodeErrorConstructor} + * + * @extends {NodeErrorConstructor} + */ +interface ErrImportAttributeTypeIncompatibleConstructor + extends NodeErrorConstructor { + /** + * Create a new `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` error. + * + * @see {@linkcode ErrImportAttributeTypeIncompatible} + * + * @param {string} id + * Module id + * @param {string} type + * Specified type + * @return {ErrImportAttributeTypeIncompatible} + */ + new (id: string, type: string): ErrImportAttributeTypeIncompatible +} + +/** + * `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` model. + * + * Thrown when an import `type` attribute is provided, but the specified module + * is of a different type. + * + * @see {@linkcode ErrImportAttributeTypeIncompatibleConstructor} + * + * @type {ErrImportAttributeTypeIncompatibleConstructor} + * + * @class + */ +const ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE: + ErrImportAttributeTypeIncompatibleConstructor = E( + codes.ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE, + TypeError, + 'Module "%s" is not of type "%s"' + ) + +export { + ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE as default, + type ErrImportAttributeTypeIncompatible, + type Args as ErrImportAttributeTypeIncompatibleArgs, + type ErrImportAttributeTypeIncompatibleConstructor +} diff --git a/src/errors/index.ts b/src/errors/index.ts index 3b8c6021..19da558f 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -34,6 +34,12 @@ export { type ErrImportAttributeMissingArgs, type ErrImportAttributeMissingConstructor } from './err-import-attribute-missing' +export { + default as ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE, + type ErrImportAttributeTypeIncompatible, + type ErrImportAttributeTypeIncompatibleArgs, + type ErrImportAttributeTypeIncompatibleConstructor +} from './err-import-attribute-type-incompatible' export { default as ERR_INCOMPATIBLE_OPTION_PAIR, type ErrIncompatibleOptionPair,