Skip to content

Commit

Permalink
errors,vm: update error and use cause
Browse files Browse the repository at this point in the history
PR-URL: #42820
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
  • Loading branch information
devsnek authored and juanarbol committed May 31, 2022
1 parent 91e804f commit 9e815c5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
17 changes: 11 additions & 6 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2816,12 +2816,6 @@ Cached data cannot be created for modules which have already been evaluated.
The module being returned from the linker function is from a different context
than the parent module. Linked modules must share the same context.

<a id="ERR_VM_MODULE_LINKING_ERRORED"></a>

### `ERR_VM_MODULE_LINKING_ERRORED`

The linker function returned a module for which linking has failed.

<a id="ERR_VM_MODULE_LINK_FAILURE"></a>

### `ERR_VM_MODULE_LINK_FAILURE`
Expand Down Expand Up @@ -3296,6 +3290,17 @@ Used when a given value is out of the accepted range.

The module must be successfully linked before instantiation.

<a id="ERR_VM_MODULE_LINKING_ERRORED"></a>

### `ERR_VM_MODULE_LINKING_ERRORED`

<!-- YAML
added: v10.0.0
removed: REPLACEME
-->

The linker function returned a module for which linking has failed.

<a id="ERR_WORKER_UNSUPPORTED_EXTENSION"></a>

### `ERR_WORKER_UNSUPPORTED_EXTENSION`
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,8 +1625,10 @@ E('ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA',
'Cached data cannot be created for a module which has been evaluated', Error);
E('ERR_VM_MODULE_DIFFERENT_CONTEXT',
'Linked modules must use the same context', Error);
E('ERR_VM_MODULE_LINKING_ERRORED',
'Linking has already failed for the provided module', Error);
E('ERR_VM_MODULE_LINK_FAILURE', function(message, cause) {
this.cause = cause;
return message;
}, Error);
E('ERR_VM_MODULE_NOT_MODULE',
'Provided module is not an instance of Module', Error);
E('ERR_VM_MODULE_STATUS', 'Module status %s', Error);
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
ERR_VM_MODULE_ALREADY_LINKED,
ERR_VM_MODULE_DIFFERENT_CONTEXT,
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA,
ERR_VM_MODULE_LINKING_ERRORED,
ERR_VM_MODULE_LINK_FAILURE,
ERR_VM_MODULE_NOT_MODULE,
ERR_VM_MODULE_STATUS,
} = require('internal/errors').codes;
Expand Down Expand Up @@ -317,9 +317,7 @@ class SourceTextModule extends Module {
throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();
}
if (module.status === 'errored') {
// TODO(devsnek): replace with ERR_VM_MODULE_LINK_FAILURE
// and error cause proposal.
throw new ERR_VM_MODULE_LINKING_ERRORED();
throw new ERR_VM_MODULE_LINK_FAILURE(`request for '${identifier}' resolved to an errored module`, module.error);
}
if (module.status === 'unlinked') {
await module[kLink](linker);
Expand Down
15 changes: 10 additions & 5 deletions test/parallel/test-vm-module-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,25 @@ async function checkLinking() {
code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'
});

const error = new Error();
await assert.rejects(async () => {
const erroredModule = new SourceTextModule('import "foo";');
globalThis.error = error;
const erroredModule = new SourceTextModule('throw error;');
await erroredModule.link(common.mustNotCall());
try {
await erroredModule.link(common.mustCall(() => ({})));
await erroredModule.evaluate();
} catch {
// ignored
} finally {
assert.strictEqual(erroredModule.status, 'errored');
}
delete globalThis.error;

assert.strictEqual(erroredModule.status, 'errored');

const rootModule = new SourceTextModule('import "errored";');
await rootModule.link(common.mustCall(() => erroredModule));
}, {
code: 'ERR_VM_MODULE_LINKING_ERRORED'
code: 'ERR_VM_MODULE_LINK_FAILURE',
cause: error,
});
}

Expand Down

0 comments on commit 9e815c5

Please sign in to comment.