Skip to content

Commit 0e16120

Browse files
devsnektargos
authored andcommitted
errors,vm: update error and use cause
PR-URL: #42820 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
1 parent 494650c commit 0e16120

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

doc/api/errors.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -2853,12 +2853,6 @@ Cached data cannot be created for modules which have already been evaluated.
28532853
The module being returned from the linker function is from a different context
28542854
than the parent module. Linked modules must share the same context.
28552855

2856-
<a id="ERR_VM_MODULE_LINKING_ERRORED"></a>
2857-
2858-
### `ERR_VM_MODULE_LINKING_ERRORED`
2859-
2860-
The linker function returned a module for which linking has failed.
2861-
28622856
<a id="ERR_VM_MODULE_LINK_FAILURE"></a>
28632857

28642858
### `ERR_VM_MODULE_LINK_FAILURE`
@@ -3344,6 +3338,17 @@ Used when a given value is out of the accepted range.
33443338

33453339
The module must be successfully linked before instantiation.
33463340

3341+
<a id="ERR_VM_MODULE_LINKING_ERRORED"></a>
3342+
3343+
### `ERR_VM_MODULE_LINKING_ERRORED`
3344+
3345+
<!-- YAML
3346+
added: v10.0.0
3347+
removed: REPLACEME
3348+
-->
3349+
3350+
The linker function returned a module for which linking has failed.
3351+
33473352
<a id="ERR_WORKER_UNSUPPORTED_EXTENSION"></a>
33483353

33493354
### `ERR_WORKER_UNSUPPORTED_EXTENSION`

lib/internal/errors.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,10 @@ E('ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA',
16531653
'Cached data cannot be created for a module which has been evaluated', Error);
16541654
E('ERR_VM_MODULE_DIFFERENT_CONTEXT',
16551655
'Linked modules must use the same context', Error);
1656-
E('ERR_VM_MODULE_LINKING_ERRORED',
1657-
'Linking has already failed for the provided module', Error);
1656+
E('ERR_VM_MODULE_LINK_FAILURE', function(message, cause) {
1657+
this.cause = cause;
1658+
return message;
1659+
}, Error);
16581660
E('ERR_VM_MODULE_NOT_MODULE',
16591661
'Provided module is not an instance of Module', Error);
16601662
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);

lib/internal/vm/module.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const {
3434
ERR_VM_MODULE_ALREADY_LINKED,
3535
ERR_VM_MODULE_DIFFERENT_CONTEXT,
3636
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA,
37-
ERR_VM_MODULE_LINKING_ERRORED,
37+
ERR_VM_MODULE_LINK_FAILURE,
3838
ERR_VM_MODULE_NOT_MODULE,
3939
ERR_VM_MODULE_STATUS,
4040
} = require('internal/errors').codes;
@@ -317,9 +317,7 @@ class SourceTextModule extends Module {
317317
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
318318
}
319319
if (module.status === 'errored') {
320-
// TODO(devsnek): replace with ERR_VM_MODULE_LINK_FAILURE
321-
// and error cause proposal.
322-
throw new ERR_VM_MODULE_LINKING_ERRORED();
320+
throw new ERR_VM_MODULE_LINK_FAILURE(`request for '${identifier}' resolved to an errored module`, module.error);
323321
}
324322
if (module.status === 'unlinked') {
325323
await module[kLink](linker);

test/parallel/test-vm-module-errors.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,25 @@ async function checkLinking() {
139139
code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'
140140
});
141141

142+
const error = new Error();
142143
await assert.rejects(async () => {
143-
const erroredModule = new SourceTextModule('import "foo";');
144+
globalThis.error = error;
145+
const erroredModule = new SourceTextModule('throw error;');
146+
await erroredModule.link(common.mustNotCall());
144147
try {
145-
await erroredModule.link(common.mustCall(() => ({})));
148+
await erroredModule.evaluate();
146149
} catch {
147150
// ignored
148-
} finally {
149-
assert.strictEqual(erroredModule.status, 'errored');
150151
}
152+
delete globalThis.error;
153+
154+
assert.strictEqual(erroredModule.status, 'errored');
151155

152156
const rootModule = new SourceTextModule('import "errored";');
153157
await rootModule.link(common.mustCall(() => erroredModule));
154158
}, {
155-
code: 'ERR_VM_MODULE_LINKING_ERRORED'
159+
code: 'ERR_VM_MODULE_LINK_FAILURE',
160+
cause: error,
156161
});
157162
}
158163

0 commit comments

Comments
 (0)