Skip to content

Commit 1ed3c54

Browse files
committed
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: #26738Fixes: #26669Fixes: #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‎

Lines changed: 14 additions & 1 deletion
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‎

Lines changed: 34 additions & 31 deletions
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+
letuseOriginalName=false;
22+
2123
// Lazily loaded
2224
letutil;
2325
letassert;
@@ -74,19 +76,7 @@ class SystemError extends Error{
7476
value: key,
7577
writable: true
7678
});
77-
}
78-
79-
getname(){
80-
return`SystemError [${this[kCode]}]`;
81-
}
82-
83-
setname(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
getcode(){
@@ -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
functionmakeSystemErrorWithCode(key){
@@ -151,8 +145,6 @@ function makeSystemErrorWithCode(key){
151145
};
152146
}
153147

154-
letuseOriginalName=false;
155-
156148
functionmakeNodeErrorWithCode(Base,key){
157149
returnclassNodeErrorextendsBase{
158150
constructor(...args){
@@ -164,22 +156,7 @@ function makeNodeErrorWithCode(Base, key){
164156
writable: true,
165157
configurable: true
166158
});
167-
}
168-
169-
getname(){
170-
if(useOriginalName){
171-
returnsuper.name;
172-
}
173-
return`${super.name} [${key}]`;
174-
}
175-
176-
setname(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
getcode(){
@@ -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+
functionaddCodeToName(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+
deleteerr.name;
200+
}
201+
}
202+
200203
// Utility function for registering the error codes. Only used here. Exported
201204
// *only* to allow for testing.
202205
functionE(sym,val,def, ...otherClasses){

‎lib/internal/http2/util.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,12 @@ class NghttpError extends Error{
498498
this.code='ERR_HTTP2_ERROR';
499499
this.name='Error [ERR_HTTP2_ERROR]';
500500
this.errno=ret;
501+
this.stack;
502+
deletethis.name;
503+
}
504+
505+
toString(){
506+
return`${this.name} [${this.code}]: ${this.message}`;
501507
}
502508
}
503509

‎test/parallel/test-assert-async.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const promises = [];
1313
constrejectingFn=async()=>assert.fail();
1414
consterrObj={
1515
code: 'ERR_ASSERTION',
16-
name: 'AssertionError [ERR_ASSERTION]',
16+
name: 'AssertionError',
1717
message: 'Failed'
1818
};
1919
// `assert.rejects` accepts a function or a promise as first argument.
@@ -38,7 +38,7 @@ const promises = [];
3838

3939
promise=assert.rejects(()=>{},common.mustNotCall());
4040
promises.push(assert.rejects(promise,{
41-
name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
41+
name: 'TypeError',
4242
code: 'ERR_INVALID_RETURN_VALUE',
4343
message: 'Expected instance of Promise to be returned '+
4444
'from the "promiseFn" function but got type undefined.'
@@ -75,7 +75,7 @@ promises.push(assert.rejects(
7575
message: 'Expected instance of Promise to be returned '+
7676
'from the "promiseFn" function but got instance of Map.',
7777
code: 'ERR_INVALID_RETURN_VALUE',
78-
name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
78+
name: 'TypeError'
7979
}));
8080
promises.push(assert.doesNotReject(async()=>{}));
8181
promises.push(assert.doesNotReject(Promise.resolve()));

‎test/parallel/test-assert-deep.js‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ assert.throws(
778778
assert.throws(
779779
()=>assert.notDeepStrictEqual(newDate(2000,3,14),newDate(2000,3,14)),
780780
{
781-
name: 'AssertionError [ERR_ASSERTION]',
781+
name: 'AssertionError',
782782
message: 'Expected "actual" not to be strictly deep-equal to: '+
783783
util.inspect(newDate(2000,3,14))
784784
}
@@ -790,35 +790,35 @@ assert.throws(
790790
()=>assert.deepStrictEqual(/ab/,/a/),
791791
{
792792
code: 'ERR_ASSERTION',
793-
name: 'AssertionError [ERR_ASSERTION]',
793+
name: 'AssertionError',
794794
message: `${defaultMsgStartFull}\n\n+ /ab/\n- /a/`
795795
});
796796
assert.throws(
797797
()=>assert.deepStrictEqual(/a/g,/a/),
798798
{
799799
code: 'ERR_ASSERTION',
800-
name: 'AssertionError [ERR_ASSERTION]',
800+
name: 'AssertionError',
801801
message: `${defaultMsgStartFull}\n\n+ /a/g\n- /a/`
802802
});
803803
assert.throws(
804804
()=>assert.deepStrictEqual(/a/i,/a/),
805805
{
806806
code: 'ERR_ASSERTION',
807-
name: 'AssertionError [ERR_ASSERTION]',
807+
name: 'AssertionError',
808808
message: `${defaultMsgStartFull}\n\n+ /a/i\n- /a/`
809809
});
810810
assert.throws(
811811
()=>assert.deepStrictEqual(/a/m,/a/),
812812
{
813813
code: 'ERR_ASSERTION',
814-
name: 'AssertionError [ERR_ASSERTION]',
814+
name: 'AssertionError',
815815
message: `${defaultMsgStartFull}\n\n+ /a/m\n- /a/`
816816
});
817817
assert.throws(
818818
()=>assert.deepStrictEqual(/a/igm,/a/im),
819819
{
820820
code: 'ERR_ASSERTION',
821-
name: 'AssertionError [ERR_ASSERTION]',
821+
name: 'AssertionError',
822822
message: `${defaultMsgStartFull}\n\n+ /a/gim\n- /a/im\n ^`
823823
});
824824

@@ -844,22 +844,22 @@ assert.deepStrictEqual({a: 4, b: '2'},{a: 4, b: '2'});
844844
assert.throws(()=>assert.deepStrictEqual([4],['4']),
845845
{
846846
code: 'ERR_ASSERTION',
847-
name: 'AssertionError [ERR_ASSERTION]',
847+
name: 'AssertionError',
848848
message: `${defaultMsgStartFull}\n\n [\n+ 4\n- '4'\n ]`
849849
});
850850
assert.throws(
851851
()=>assert.deepStrictEqual({a: 4},{a: 4,b: true}),
852852
{
853853
code: 'ERR_ASSERTION',
854-
name: 'AssertionError [ERR_ASSERTION]',
854+
name: 'AssertionError',
855855
message: `${defaultMsgStartFull}\n\n `+
856856
'{\n a: 4,\n- b: true\n }'
857857
});
858858
assert.throws(
859859
()=>assert.deepStrictEqual(['a'],{0: 'a'}),
860860
{
861861
code: 'ERR_ASSERTION',
862-
name: 'AssertionError [ERR_ASSERTION]',
862+
name: 'AssertionError',
863863
message: `${defaultMsgStartFull}\n\n`+
864864
"+ [\n+ 'a'\n+ ]\n-{\n- '0': 'a'\n- }"
865865
});
@@ -953,7 +953,7 @@ assert.deepStrictEqual(obj1, obj2);
953953
()=>assert.deepStrictEqual(a,b),
954954
{
955955
code: 'ERR_ASSERTION',
956-
name: 'AssertionError [ERR_ASSERTION]',
956+
name: 'AssertionError',
957957
message: /\.\.\./g
958958
}
959959
);
@@ -977,7 +977,7 @@ assert.throws(
977977
()=>assert.deepStrictEqual([1,2,3],[1,2]),
978978
{
979979
code: 'ERR_ASSERTION',
980-
name: 'AssertionError [ERR_ASSERTION]',
980+
name: 'AssertionError',
981981
message: `${defaultMsgStartFull}\n\n`+
982982
' [\n'+
983983
' 1,\n'+
@@ -1063,7 +1063,7 @@ assert.throws(
10631063
()=>assert.deepStrictEqual(a,b),
10641064
{
10651065
code: 'ERR_ASSERTION',
1066-
name: 'AssertionError [ERR_ASSERTION]',
1066+
name: 'AssertionError',
10671067
message: /a:\[Getter:5]\n-a:\[Getter:6]\n/
10681068
}
10691069
);

‎test/parallel/test-assert-fail-deprecation.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ assert.throws(() =>{
1515
assert.fail('first','second');
1616
},{
1717
code: 'ERR_ASSERTION',
18-
name: 'AssertionError [ERR_ASSERTION]',
18+
name: 'AssertionError',
1919
message: '\'first\' != \'second\'',
2020
operator: '!=',
2121
actual: 'first',
@@ -28,7 +28,7 @@ assert.throws(() =>{
2828
assert.fail('ignored','ignored','another custom message');
2929
},{
3030
code: 'ERR_ASSERTION',
31-
name: 'AssertionError [ERR_ASSERTION]',
31+
name: 'AssertionError',
3232
message: 'another custom message',
3333
operator: 'fail',
3434
actual: 'ignored',
@@ -49,7 +49,7 @@ assert.throws(() =>{
4949
assert.fail('first','second',undefined,'operator');
5050
},{
5151
code: 'ERR_ASSERTION',
52-
name: 'AssertionError [ERR_ASSERTION]',
52+
name: 'AssertionError',
5353
message: '\'first\' operator \'second\'',
5454
operator: 'operator',
5555
actual: 'first',

‎test/parallel/test-assert-fail.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ assert.throws(
88
()=>{assert.fail();},
99
{
1010
code: 'ERR_ASSERTION',
11-
name: 'AssertionError [ERR_ASSERTION]',
11+
name: 'AssertionError',
1212
message: 'Failed',
1313
operator: 'fail',
1414
actual: undefined,
@@ -22,7 +22,7 @@ assert.throws(() =>{
2222
assert.fail('custom message');
2323
},{
2424
code: 'ERR_ASSERTION',
25-
name: 'AssertionError [ERR_ASSERTION]',
25+
name: 'AssertionError',
2626
message: 'custom message',
2727
operator: 'fail',
2828
actual: undefined,

‎test/parallel/test-assert-first-line.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const{path } = require('../common/fixtures');
99
assert.throws(
1010
()=>require(path('assert-first-line')),
1111
{
12-
name: 'AssertionError [ERR_ASSERTION]',
12+
name: 'AssertionError',
1313
message: "The expression evaluated to a falsy value:\n\n ässört.ok('')\n"
1414
}
1515
);
1616

1717
assert.throws(
1818
()=>require(path('assert-long-line')),
1919
{
20-
name: 'AssertionError [ERR_ASSERTION]',
20+
name: 'AssertionError',
2121
message: "The expression evaluated to a falsy value:\n\n assert.ok('')\n"
2222
}
2323
);

0 commit comments

Comments
(0)