esm: rename error code related to import attributes · nodejs/node@7740bf8 · GitHub
Skip to content

Commit 7740bf8

Browse files
aduh95targos
authored andcommitted
esm: rename error code related to import attributes
PR-URL: #50181 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 143ddde commit 7740bf8

6 files changed

Lines changed: 71 additions & 52 deletions

File tree

doc/api/errors.md

Lines changed: 45 additions & 20 deletions

lib/internal/errors.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,15 +1280,10 @@ E('ERR_HTTP_SOCKET_ENCODING',
12801280
E('ERR_HTTP_TRAILER_INVALID',
12811281
'Trailers are invalid with this transfer encoding', Error);
12821282
E('ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor', TypeError);
1283-
// TODO(aduh95): change the error to mention import attributes instead of import assertions.
1284-
E('ERR_IMPORT_ASSERTION_TYPE_FAILED',
1283+
E('ERR_IMPORT_ATTRIBUTE_MISSING',
1284+
'Module "%s" needs an import attribute of "%s: %s"', TypeError);
1285+
E('ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE',
12851286
'Module "%s" is not of type "%s"', TypeError);
1286-
// TODO(aduh95): change the error to mention import attributes instead of import assertions.
1287-
E('ERR_IMPORT_ASSERTION_TYPE_MISSING',
1288-
'Module "%s" needs an import attribute of type "%s"', TypeError);
1289-
// TODO(aduh95): change the error to mention import attributes instead of import assertions.
1290-
E('ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED',
1291-
'Import attribute type "%s" is unsupported', TypeError);
12921287
E('ERR_IMPORT_ATTRIBUTE_UNSUPPORTED',
12931288
'Import attribute "%s" with value "%s" is not supported', TypeError);
12941289
E('ERR_INCOMPATIBLE_OPTION_PAIR',

lib/internal/modules/esm/assert.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ const {
1010
const { validateString } = require('internal/validators');
1111

1212
const {
13-
ERR_IMPORT_ASSERTION_TYPE_FAILED,
14-
ERR_IMPORT_ASSERTION_TYPE_MISSING,
15-
ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED,
13+
ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE,
14+
ERR_IMPORT_ATTRIBUTE_MISSING,
1615
ERR_IMPORT_ATTRIBUTE_UNSUPPORTED,
1716
} = require('internal/errors').codes;
1817

@@ -86,7 +85,7 @@ function validateAttributes(url, format,
8685
// `importAttributes.type` might not have been it.
8786
if (!ObjectPrototypeHasOwnProperty(importAttributes, 'type')) {
8887
// `type` wasn't specified at all.
89-
throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, validType);
88+
throw new ERR_IMPORT_ATTRIBUTE_MISSING(url, 'type', validType);
9089
}
9190
return handleInvalidType(url, importAttributes.type);
9291
}
@@ -103,11 +102,11 @@ function handleInvalidType(url, type) {
103102

104103
// `type` might not have been one of the types we understand.
105104
if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) {
106-
throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(type);
105+
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type);
107106
}
108107

109108
// `type` was the wrong value for this format.
110-
throw new ERR_IMPORT_ASSERTION_TYPE_FAILED(url, type);
109+
throw new ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE(url, type);
111110
}
112111

113112

test/es-module/test-esm-import-attributes-errors.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,37 @@ async function test() {
1818

1919
await rejects(
2020
import(`data:text/javascript,import${JSON.stringify(jsModuleDataUrl)}with{type:"json"}`),
21-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
21+
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
2222
);
2323

2424
await rejects(
2525
import(jsModuleDataUrl, { with: { type: 'json' } }),
26-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
26+
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
2727
);
2828

2929
await rejects(
3030
import(jsModuleDataUrl, { with: { type: 'unsupported' } }),
31-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
31+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
3232
);
3333

3434
await rejects(
3535
import(jsonModuleDataUrl),
36-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
36+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
3737
);
3838

3939
await rejects(
4040
import(jsonModuleDataUrl, { with: {} }),
41-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
41+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
4242
);
4343

4444
await rejects(
4545
import(jsonModuleDataUrl, { with: { foo: 'bar' } }),
46-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
46+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
4747
);
4848

4949
await rejects(
5050
import(jsonModuleDataUrl, { with: { type: 'unsupported' } }),
51-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
51+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
5252
);
5353
}
5454

test/es-module/test-esm-import-attributes-errors.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,35 @@ await rejects(
1313

1414
await rejects(
1515
import(`data:text/javascript,import${JSON.stringify(jsModuleDataUrl)}with{type:"json"}`),
16-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
16+
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
1717
);
1818

1919
await rejects(
2020
import(jsModuleDataUrl, { with: { type: 'json' } }),
21-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED' }
21+
{ code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' }
2222
);
2323

2424
await rejects(
2525
import(import.meta.url, { with: { type: 'unsupported' } }),
26-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
26+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
2727
);
2828

2929
await rejects(
3030
import(jsonModuleDataUrl),
31-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
31+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
3232
);
3333

3434
await rejects(
3535
import(jsonModuleDataUrl, { with: {} }),
36-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
36+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
3737
);
3838

3939
await rejects(
4040
import(jsonModuleDataUrl, { with: { foo: 'bar' } }),
41-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING' }
41+
{ code: 'ERR_IMPORT_ATTRIBUTE_MISSING' }
4242
);
4343

4444
await rejects(
4545
import(jsonModuleDataUrl, { with: { type: 'unsupported' } }),
46-
{ code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' }
46+
{ code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' }
4747
);

test/es-module/test-esm-import-attributes-validation.js

Lines changed: 4 additions & 4 deletions

0 commit comments

Comments
 (0)