From 8b2c77cca2b8dfebe4c3f392e8812a7c70666d48 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 14 Oct 2023 08:28:33 +0200 Subject: [PATCH 1/3] esm: rename error code related to import attributes --- doc/api/errors.md | 66 +++++++++++++------ lib/internal/errors.js | 11 +--- lib/internal/modules/esm/assert.js | 11 ++-- .../test-esm-import-attributes-errors.js | 14 ++-- .../test-esm-import-attributes-errors.mjs | 14 ++-- .../test-esm-import-attributes-validation.js | 8 +-- 6 files changed, 72 insertions(+), 52 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 6051437436a551..293c8e4cb5692d 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1749,43 +1749,29 @@ is set for the `Http2Stream`. An attempt was made to construct an object using a non-public constructor. - + -### `ERR_IMPORT_ASSERTION_TYPE_FAILED` +### `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE` An import `type` attribute was provided, but the specified module is of a different type. - + -### `ERR_IMPORT_ASSERTION_TYPE_MISSING` +### `ERR_IMPORT_ATTRIBUTE_MISSING` An import attribute is missing, preventing the specified module to be imported. - - -### `ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED` - - - -An import attribute is not supported by this version of Node.js. - ### `ERR_IMPORT_ATTRIBUTE_UNSUPPORTED` @@ -3302,6 +3288,46 @@ changes: An invalid transfer object was passed to `postMessage()`. + + +### `ERR_IMPORT_ASSERTION_TYPE_FAILED` + + + +An import assertion has failed, preventing the specified module to be imported. + + + +### `ERR_IMPORT_ASSERTION_TYPE_MISSING` + + + +An import assertion is missing, preventing the specified module to be imported. + + + +### `ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED` + + + +An import attribute is not supported by this version of Node.js. + + ### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 76ce7867043c7d..30de7e7b56eca9 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1280,15 +1280,10 @@ E('ERR_HTTP_SOCKET_ENCODING', E('ERR_HTTP_TRAILER_INVALID', 'Trailers are invalid with this transfer encoding', Error); E('ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor', TypeError); -// TODO(aduh95): change the error to mention import attributes instead of import assertions. -E('ERR_IMPORT_ASSERTION_TYPE_FAILED', +E('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', 'Module "%s" is not of type "%s"', TypeError); -// TODO(aduh95): change the error to mention import attributes instead of import assertions. -E('ERR_IMPORT_ASSERTION_TYPE_MISSING', - 'Module "%s" needs an import attribute of type "%s"', TypeError); -// TODO(aduh95): change the error to mention import attributes instead of import assertions. -E('ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED', - 'Import attribute type "%s" is unsupported', TypeError); +E('ERR_IMPORT_ATTRIBUTE_MISSING', + 'Module "%s" needs an import attribute of "%s: %s"', TypeError); E('ERR_IMPORT_ATTRIBUTE_UNSUPPORTED', 'Import attribute "%s" with value "%s" is not supported', TypeError); E('ERR_INCOMPATIBLE_OPTION_PAIR', diff --git a/lib/internal/modules/esm/assert.js b/lib/internal/modules/esm/assert.js index ce3280de84bf4d..5672f8c8f9959d 100644 --- a/lib/internal/modules/esm/assert.js +++ b/lib/internal/modules/esm/assert.js @@ -10,9 +10,8 @@ const { const { validateString } = require('internal/validators'); const { - ERR_IMPORT_ASSERTION_TYPE_FAILED, - ERR_IMPORT_ASSERTION_TYPE_MISSING, - ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED, + ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE, + ERR_IMPORT_ATTRIBUTE_MISSING, ERR_IMPORT_ATTRIBUTE_UNSUPPORTED, } = require('internal/errors').codes; @@ -86,7 +85,7 @@ function validateAttributes(url, format, // `importAttributes.type` might not have been it. if (!ObjectPrototypeHasOwnProperty(importAttributes, 'type')) { // `type` wasn't specified at all. - throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, validType); + throw new ERR_IMPORT_ATTRIBUTE_MISSING(url, 'type', validType); } return handleInvalidType(url, importAttributes.type); } @@ -103,11 +102,11 @@ function handleInvalidType(url, type) { // `type` might not have been one of the types we understand. if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) { - throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(type); + throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type); } // `type` was the wrong value for this format. - throw new ERR_IMPORT_ASSERTION_TYPE_FAILED(url, type); + throw new ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE(url, type); } diff --git a/test/es-module/test-esm-import-attributes-errors.js b/test/es-module/test-esm-import-attributes-errors.js index 4521248db176e5..243277fdcd7d29 100644 --- a/test/es-module/test-esm-import-attributes-errors.js +++ b/test/es-module/test-esm-import-attributes-errors.js @@ -18,37 +18,37 @@ async function test() { await rejects( import(`data:text/javascript,import${JSON.stringify(jsModuleDataUrl)}with{type:"json"}`), - { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); await rejects( import(jsModuleDataUrl, { with: { type: 'json' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); await rejects( import(jsModuleDataUrl, { with: { type: 'unsupported' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects( import(jsonModuleDataUrl), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: {} }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: { foo: 'bar' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: { type: 'unsupported' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); } diff --git a/test/es-module/test-esm-import-attributes-errors.mjs b/test/es-module/test-esm-import-attributes-errors.mjs index ff932636e39a5f..c29cc459326c4b 100644 --- a/test/es-module/test-esm-import-attributes-errors.mjs +++ b/test/es-module/test-esm-import-attributes-errors.mjs @@ -13,35 +13,35 @@ await rejects( await rejects( import(`data:text/javascript,import${JSON.stringify(jsModuleDataUrl)}with{type:"json"}`), - { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); await rejects( import(jsModuleDataUrl, { with: { type: 'json' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' } + { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } ); await rejects( import(import.meta.url, { with: { type: 'unsupported' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects( import(jsonModuleDataUrl), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: {} }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: { foo: 'bar' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } ); await rejects( import(jsonModuleDataUrl, { with: { type: 'unsupported' } }), - { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); diff --git a/test/es-module/test-esm-import-attributes-validation.js b/test/es-module/test-esm-import-attributes-validation.js index f436f7073126d7..0d1c4bb57d92cf 100644 --- a/test/es-module/test-esm-import-attributes-validation.js +++ b/test/es-module/test-esm-import-attributes-validation.js @@ -15,7 +15,7 @@ assert.ok(validateAttributes(url, 'module', {})); assert.ok(validateAttributes(url, 'wasm', {})); assert.throws(() => validateAttributes(url, 'json', {}), { - code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING', + code: 'ERR_IMPORT_ATTRIBUTE_MISSING', }); assert.throws(() => validateAttributes(url, 'json', { type: 'json', unsupportedAttribute: 'value' }), { @@ -27,17 +27,17 @@ assert.throws(() => validateAttributes(url, 'module', { unsupportedAttribute: 'v }); assert.throws(() => validateAttributes(url, 'module', { type: 'json' }), { - code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED', + code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', }); // The HTML spec specifically disallows this for now, while Wasm module import // and whether it will require a type assertion is still an open question. assert.throws(() => validateAttributes(url, 'module', { type: 'javascript' }), { - code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED', + code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED', }); assert.throws(() => validateAttributes(url, 'module', { type: 'css' }), { - code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED', + code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED', }); assert.throws(() => validateAttributes(url, 'module', { type: false }), { From fe5bef66e72c65b6803f252d1939ad93a2cccfe3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 14 Oct 2023 12:23:25 +0200 Subject: [PATCH 2/3] fixup! esm: rename error code related to import attributes --- lib/internal/errors.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 30de7e7b56eca9..1925bf72c33f2f 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1280,10 +1280,10 @@ E('ERR_HTTP_SOCKET_ENCODING', E('ERR_HTTP_TRAILER_INVALID', 'Trailers are invalid with this transfer encoding', Error); E('ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor', TypeError); -E('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', - 'Module "%s" is not of type "%s"', TypeError); E('ERR_IMPORT_ATTRIBUTE_MISSING', 'Module "%s" needs an import attribute of "%s: %s"', TypeError); +E('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE', + 'Module "%s" is not of type "%s"', TypeError); E('ERR_IMPORT_ATTRIBUTE_UNSUPPORTED', 'Import attribute "%s" with value "%s" is not supported', TypeError); E('ERR_INCOMPATIBLE_OPTION_PAIR', From 945456d411805747b1dbdb6b6ad0fb3cd17da1ff Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 14 Oct 2023 17:19:44 +0200 Subject: [PATCH 3/3] fixup! esm: rename error code related to import attributes --- doc/api/errors.md | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 293c8e4cb5692d..ee767565568724 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -3327,7 +3327,6 @@ removed: REPLACEME An import attribute is not supported by this version of Node.js. - ### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`