From 37d4f08cbd38c723fee0cc36c811ec3c2930a2e9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Oct 2023 16:27:55 +0200 Subject: [PATCH] esm: rename error code related to import attributes PR-URL: https://github.com/nodejs/node/pull/50181 Reviewed-By: Geoffrey Booth Reviewed-By: Zeyu "Alex" Yang --- doc/api/errors.md | 65 +++++++++++++------ 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, 71 insertions(+), 52 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 8070fffc388b9f..9ff60a01977693 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` @@ -3308,6 +3294,45 @@ 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 a72236063b47fd..363b9d3bb8fe8b 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_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); -// 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_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 }), {