Skip to content

Commit

Permalink
lib: remove NodeError from the prototype of errors with code
Browse files Browse the repository at this point in the history
Signed-off-by: Michaël Zasso <targos@protonmail.com>

PR-URL: #33857
Refs: #33770
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
targos authored and addaleax committed Jun 19, 2020
1 parent 646e5a4 commit a86a295
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 28 deletions.
52 changes: 28 additions & 24 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
4 changes: 2 additions & 2 deletions test/message/esm_loader_not_found.out
Original file line number Diff line number Diff line change
Expand Up @@ -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:*:*)
Expand All @@ -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'
}
1 change: 1 addition & 0 deletions test/message/esm_loader_not_found_cjs_hint_bare.out
Original file line number Diff line number Diff line change
Expand Up @@ -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:*:*)
Expand Down
4 changes: 2 additions & 2 deletions test/message/esm_loader_not_found_cjs_hint_relative.out
Original file line number Diff line number Diff line change
Expand Up @@ -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:*:*)
Expand All @@ -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'
}
1 change: 1 addition & 0 deletions test/message/internal_assert.out
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
1 change: 1 addition & 0 deletions test/message/internal_assert_fail.out
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down

0 comments on commit a86a295

Please sign in to comment.