Skip to content

Commit 1ed3c54

Browse files
committedMar 23, 2019
errors: update error name
This updates all Node.js errors by removing the `code` being part of the `name` property. Instead, the name is just changed once on instantiation, the stack is accessed to create the stack as expected and then the `name` property is set back to it's original form. PR-URL: #26738 Fixes: #26669 Fixes: #20253 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent c757cb1 commit 1ed3c54

File tree

71 files changed

+283
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+283
-284
lines changed
 

‎lib/internal/assert/assertion_error.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,25 @@ class AssertionError extends Error {
388388
}
389389

390390
this.generatedMessage = !message;
391-
this.name = 'AssertionError [ERR_ASSERTION]';
391+
Object.defineProperty(this, 'name', {
392+
value: 'AssertionError [ERR_ASSERTION]',
393+
enumerable: false,
394+
writable: true,
395+
configurable: true
396+
});
392397
this.code = 'ERR_ASSERTION';
393398
this.actual = actual;
394399
this.expected = expected;
395400
this.operator = operator;
396401
Error.captureStackTrace(this, stackStartFn);
402+
// Create error message including the error code in the name.
403+
this.stack;
404+
// Reset the name.
405+
this.name = 'AssertionError';
406+
}
407+
408+
toString() {
409+
return `${this.name} [${this.code}]: ${this.message}`;
397410
}
398411

399412
[inspect.custom](recurseTimes, ctx) {

‎lib/internal/errors.js

+34-31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const codes = {};
1818
const { kMaxLength } = internalBinding('buffer');
1919
const { defineProperty } = Object;
2020

21+
let useOriginalName = false;
22+
2123
// Lazily loaded
2224
let util;
2325
let assert;
@@ -74,19 +76,7 @@ class SystemError extends Error {
7476
value: key,
7577
writable: true
7678
});
77-
}
78-
79-
get name() {
80-
return `SystemError [${this[kCode]}]`;
81-
}
82-
83-
set name(value) {
84-
defineProperty(this, 'name', {
85-
configurable: true,
86-
enumerable: true,
87-
value,
88-
writable: true
89-
});
79+
addCodeToName(this, 'SystemError', key);
9080
}
9181

9282
get code() {
@@ -141,6 +131,10 @@ class SystemError extends Error {
141131
this[kInfo].dest = val ?
142132
lazyBuffer().from(val.toString()) : undefined;
143133
}
134+
135+
toString() {
136+
return `${this.name} [${this.code}]: ${this.message}`;
137+
}
144138
}
145139

146140
function makeSystemErrorWithCode(key) {
@@ -151,8 +145,6 @@ function makeSystemErrorWithCode(key) {
151145
};
152146
}
153147

154-
let useOriginalName = false;
155-
156148
function makeNodeErrorWithCode(Base, key) {
157149
return class NodeError extends Base {
158150
constructor(...args) {
@@ -164,22 +156,7 @@ function makeNodeErrorWithCode(Base, key) {
164156
writable: true,
165157
configurable: true
166158
});
167-
}
168-
169-
get name() {
170-
if (useOriginalName) {
171-
return super.name;
172-
}
173-
return `${super.name} [${key}]`;
174-
}
175-
176-
set name(value) {
177-
defineProperty(this, 'name', {
178-
configurable: true,
179-
enumerable: true,
180-
value,
181-
writable: true
182-
});
159+
addCodeToName(this, super.name, key);
183160
}
184161

185162
get code() {
@@ -194,9 +171,35 @@ function makeNodeErrorWithCode(Base, key) {
194171
writable: true
195172
});
196173
}
174+
175+
toString() {
176+
return `${this.name} [${key}]: ${this.message}`;
177+
}
197178
};
198179
}
199180

181+
function addCodeToName(err, name, code) {
182+
if (useOriginalName) {
183+
return;
184+
}
185+
// Add the error code to the name to include it in the stack trace.
186+
err.name = `${name} [${code}]`;
187+
// Access the stack to generate the error message including the error code
188+
// from the name.
189+
err.stack;
190+
// Reset the name to the actual name.
191+
if (name === 'SystemError') {
192+
defineProperty(err, 'name', {
193+
value: name,
194+
enumerable: false,
195+
writable: true,
196+
configurable: true
197+
});
198+
} else {
199+
delete err.name;
200+
}
201+
}
202+
200203
// Utility function for registering the error codes. Only used here. Exported
201204
// *only* to allow for testing.
202205
function E(sym, val, def, ...otherClasses) {

0 commit comments

Comments
 (0)
Please sign in to comment.