From 12c432565f57fdc4dc7fc2a638821ddaf11be69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 12 Jun 2020 16:04:18 +0200 Subject: [PATCH] lib: remove NodeError from the prototype of errors with code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michaƫl Zasso --- lib/internal/errors.js | 52 ++++++++++--------- test/message/esm_loader_not_found.out | 4 +- .../esm_loader_not_found_cjs_hint_bare.out | 1 + ...esm_loader_not_found_cjs_hint_relative.out | 4 +- test/message/internal_assert.out | 1 + test/message/internal_assert_fail.out | 1 + 6 files changed, 35 insertions(+), 28 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 9f5d8403f65d92..42a45fa6dbbc2e 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -248,31 +248,35 @@ function makeSystemErrorWithCode(key) { } function makeNodeErrorWithCode(Base, key) { - return class NodeError extends Base { - constructor(...args) { - if (excludedStackFn === undefined) { - super(); - } else { - const limit = Error.stackTraceLimit; - Error.stackTraceLimit = 0; - super(); - // Reset the limit and setting the name property. - Error.stackTraceLimit = limit; - } - const message = getMessage(key, args, this); - ObjectDefineProperty(this, 'message', { - value: message, - enumerable: false, - writable: true, - configurable: true - }); - addCodeToName(this, super.name, key); - this.code = key; - } - - toString() { - return `${this.name} [${key}]: ${this.message}`; + return function NodeError(...args) { + let error; + if (excludedStackFn === undefined) { + error = new Base(); + } else { + const limit = Error.stackTraceLimit; + Error.stackTraceLimit = 0; + error = new Base(); + // Reset the limit and setting the name property. + Error.stackTraceLimit = limit; } + const message = getMessage(key, args, error); + ObjectDefineProperty(error, 'message', { + value: message, + enumerable: false, + writable: true, + configurable: true, + }); + ObjectDefineProperty(error, 'toString', { + value() { + return `${this.name} [${key}]: ${this.message}`; + }, + enumerable: false, + writable: true, + configurable: true, + }); + addCodeToName(error, Base.name, key); + error.code = key; + return error; }; } diff --git a/test/message/esm_loader_not_found.out b/test/message/esm_loader_not_found.out index 1d2aa957150082..967acb648e4ae2 100644 --- a/test/message/esm_loader_not_found.out +++ b/test/message/esm_loader_not_found.out @@ -4,6 +4,7 @@ internal/modules/run_main.js:* internalBinding('errors').triggerUncaughtException( ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from * + at new NodeError (internal/errors.js:*:*) at packageResolve (internal/modules/esm/resolve.js:*:*) at moduleResolve (internal/modules/esm/resolve.js:*:*) at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:*:*) @@ -12,7 +13,6 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'i-dont-exist' imported from * at Loader.import (internal/modules/esm/loader.js:*:*) at internal/process/esm_loader.js:*:* at Object.initializeLoader (internal/process/esm_loader.js:*:*) - at runMainESM (internal/modules/run_main.js:*:*) - at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) { + at runMainESM (internal/modules/run_main.js:*:*) { code: 'ERR_MODULE_NOT_FOUND' } diff --git a/test/message/esm_loader_not_found_cjs_hint_bare.out b/test/message/esm_loader_not_found_cjs_hint_bare.out index e56f1da0f6e76e..543c3e4a4a8133 100644 --- a/test/message/esm_loader_not_found_cjs_hint_bare.out +++ b/test/message/esm_loader_not_found_cjs_hint_bare.out @@ -4,6 +4,7 @@ internal/modules/run_main.js:* Error [ERR_MODULE_NOT_FOUND]: Cannot find module '*test*fixtures*node_modules*some_module*obj' imported from *test*fixtures*esm_loader_not_found_cjs_hint_bare.mjs Did you mean to import some_module/obj.js? + at new NodeError (internal/errors.js:*:*) at finalizeResolution (internal/modules/esm/resolve.js:*:*) at packageResolve (internal/modules/esm/resolve.js:*:*) at moduleResolve (internal/modules/esm/resolve.js:*:*) diff --git a/test/message/esm_loader_not_found_cjs_hint_relative.out b/test/message/esm_loader_not_found_cjs_hint_relative.out index 76df3163bb728c..20d81355e91f21 100644 --- a/test/message/esm_loader_not_found_cjs_hint_relative.out +++ b/test/message/esm_loader_not_found_cjs_hint_relative.out @@ -6,6 +6,7 @@ internal/modules/run_main.js:* Error [ERR_MODULE_NOT_FOUND]: Cannot find module '*test*common*fixtures' imported from * Did you mean to import ./test/common/fixtures.js? + at new NodeError (internal/errors.js:*:*) at finalizeResolution (internal/modules/esm/resolve.js:*:*) at moduleResolve (internal/modules/esm/resolve.js:*:*) at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:*:*) @@ -14,7 +15,6 @@ Did you mean to import ./test/common/fixtures.js? at Loader.import (internal/modules/esm/loader.js:*:*) at internal/process/esm_loader.js:*:* at Object.initializeLoader (internal/process/esm_loader.js:*:*) - at runMainESM (internal/modules/run_main.js:*:*) - at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:*:*) { + at runMainESM (internal/modules/run_main.js:*:*) { code: 'ERR_MODULE_NOT_FOUND' } diff --git a/test/message/internal_assert.out b/test/message/internal_assert.out index cf09fdcb605269..9ca8350756c9ad 100644 --- a/test/message/internal_assert.out +++ b/test/message/internal_assert.out @@ -5,6 +5,7 @@ internal/assert.js:* Error [ERR_INTERNAL_ASSERTION]: This is caused by either a bug in Node.js or incorrect usage of Node.js internals. Please open an issue with this stack trace at https://github.com/nodejs/node/issues + at new NodeError (internal/errors.js:*:*) at assert (internal/assert.js:*:*) at * (*test*message*internal_assert.js:7:1) at * diff --git a/test/message/internal_assert_fail.out b/test/message/internal_assert_fail.out index 11b532b7b2af3c..11e253703170d2 100644 --- a/test/message/internal_assert_fail.out +++ b/test/message/internal_assert_fail.out @@ -6,6 +6,7 @@ Error [ERR_INTERNAL_ASSERTION]: Unreachable! This is caused by either a bug in Node.js or incorrect usage of Node.js internals. Please open an issue with this stack trace at https://github.com/nodejs/node/issues + at new NodeError (internal/errors.js:*:*) at Function.fail (internal/assert.js:*:*) at * (*test*message*internal_assert_fail.js:7:8) at *