esm: rename error code related to import attributes

PR-URL: https://github.com/nodejs/node/pull/50181
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
This commit is contained in:
Antoine du Hamel 2023-10-18 16:27:55 +02:00 committed by GitHub
parent dab9505829
commit 37d4f08cbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 52 deletions

View File

@ -1749,43 +1749,29 @@ is set for the `Http2Stream`.
An attempt was made to construct an object using a non-public constructor.
<a id="ERR_IMPORT_ASSERTION_TYPE_FAILED"></a>
<a id="ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE"></a>
### `ERR_IMPORT_ASSERTION_TYPE_FAILED`
### `ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE`
<!-- YAML
added:
- v17.1.0
- v16.14.0
- REPLACEME
-->
An import `type` attribute was provided, but the specified module is of a
different type.
<a id="ERR_IMPORT_ASSERTION_TYPE_MISSING"></a>
<a id="ERR_IMPORT_ATTRIBUTE_MISSING"></a>
### `ERR_IMPORT_ASSERTION_TYPE_MISSING`
### `ERR_IMPORT_ATTRIBUTE_MISSING`
<!-- YAML
added:
- v17.1.0
- v16.14.0
- REPLACEME
-->
An import attribute is missing, preventing the specified module to be imported.
<a id="ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED"></a>
### `ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED`
<!-- YAML
added:
- v17.1.0
- v16.14.0
-->
An import attribute is not supported by this version of Node.js.
<a id="ERR_IMPORT_ATTRIBUTE_UNSUPPORTED"></a>
### `ERR_IMPORT_ATTRIBUTE_UNSUPPORTED`
@ -3308,6 +3294,45 @@ changes:
An invalid transfer object was passed to `postMessage()`.
<a id="ERR_IMPORT_ASSERTION_TYPE_FAILED"></a>
### `ERR_IMPORT_ASSERTION_TYPE_FAILED`
<!-- YAML
added:
- v17.1.0
- v16.14.0
removed: REPLACEME
-->
An import assertion has failed, preventing the specified module to be imported.
<a id="ERR_IMPORT_ASSERTION_TYPE_MISSING"></a>
### `ERR_IMPORT_ASSERTION_TYPE_MISSING`
<!-- YAML
added:
- v17.1.0
- v16.14.0
removed: REPLACEME
-->
An import assertion is missing, preventing the specified module to be imported.
<a id="ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED"></a>
### `ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED`
<!-- YAML
added:
- v17.1.0
- v16.14.0
removed: REPLACEME
-->
An import attribute is not supported by this version of Node.js.
<a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a>
### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`

View File

@ -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',

View File

@ -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);
}

View File

@ -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' }
);
}

View File

@ -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' }
);

View File

@ -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 }), {