From 9e815c56fbcc7683e35bd0729a49a3652bea5417 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Thu, 21 Apr 2022 21:08:16 -0500 Subject: [PATCH] errors,vm: update error and use cause PR-URL: https://github.com/nodejs/node/pull/42820 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel Reviewed-By: Darshan Sen Reviewed-By: Mohammed Keyvanzadeh --- doc/api/errors.md | 17 +++++++++++------ lib/internal/errors.js | 6 ++++-- lib/internal/vm/module.js | 6 ++---- test/parallel/test-vm-module-errors.js | 15 ++++++++++----- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index ecb76af85ab03c..94c8d7157bf9f5 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -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. - - -### `ERR_VM_MODULE_LINKING_ERRORED` - -The linker function returned a module for which linking has failed. - ### `ERR_VM_MODULE_LINK_FAILURE` @@ -3296,6 +3290,17 @@ Used when a given value is out of the accepted range. The module must be successfully linked before instantiation. + + +### `ERR_VM_MODULE_LINKING_ERRORED` + + + +The linker function returned a module for which linking has failed. + ### `ERR_WORKER_UNSUPPORTED_EXTENSION` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 150b9608bd0f58..4a55e84eeb1d02 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -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); diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index 911b8f4426e586..eee629dda4fd05 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -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; @@ -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); diff --git a/test/parallel/test-vm-module-errors.js b/test/parallel/test-vm-module-errors.js index 888250cef84f6f..bec8258a4145c7 100644 --- a/test/parallel/test-vm-module-errors.js +++ b/test/parallel/test-vm-module-errors.js @@ -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, }); }