Skip to content

Commit d69d06b

Browse files
joyeecheungtargos
authored andcommitted
errors: add useOriginalName to internal/errors
This allows us to tell the type of the errors without using instanceof, which is necessary in WPT harness. PR-URL: #22556 Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent f0679d9 commit d69d06b

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎lib/internal/errors.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,18 @@ function makeSystemErrorWithCode(key) {
151151
};
152152
}
153153

154+
letuseOriginalName=false;
155+
154156
functionmakeNodeErrorWithCode(Base,key){
155157
returnclassNodeErrorextendsBase{
156158
constructor(...args){
157159
super(getMessage(key,args));
158160
}
159161

160162
getname(){
163+
if(useOriginalName){
164+
returnsuper.name;
165+
}
161166
return`${super.name} [${key}]`;
162167
}
163168

@@ -439,7 +444,12 @@ module.exports = {
439444
getMessage,
440445
SystemError,
441446
codes,
442-
E // This is exported only to facilitate testing.
447+
// This is exported only to facilitate testing.
448+
E,
449+
// This allows us to tell the type of the errors without using
450+
// instanceof, which is necessary in WPT harness.
451+
getuseOriginalName(){returnuseOriginalName;},
452+
setuseOriginalName(value){useOriginalName=value;}
443453
};
444454

445455
// To declare an error message, use the E(sym, val, def) function above. The sym
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Flags: --expose-internals
2+
3+
'use strict';
4+
5+
// This tests `internal/errors.useOriginalName`
6+
// This testing feature is needed to allows us to assert the types of
7+
// errors without using instanceof, which is necessary in WPT harness.
8+
// Refs: https://github.com/nodejs/node/pull/22556
9+
10+
require('../common');
11+
constassert=require('assert');
12+
consterrors=require('internal/errors');
13+
14+
15+
errors.E('TEST_ERROR_1','Error for testing purposes: %s',
16+
Error);
17+
{
18+
consterr=newerrors.codes.TEST_ERROR_1('test');
19+
assert(errinstanceofError);
20+
assert.strictEqual(err.name,'Error [TEST_ERROR_1]');
21+
}
22+
23+
{
24+
errors.useOriginalName=true;
25+
consterr=newerrors.codes.TEST_ERROR_1('test');
26+
assert(errinstanceofError);
27+
assert.strictEqual(err.name,'Error');
28+
}
29+
30+
{
31+
errors.useOriginalName=false;
32+
consterr=newerrors.codes.TEST_ERROR_1('test');
33+
assert(errinstanceofError);
34+
assert.strictEqual(err.name,'Error [TEST_ERROR_1]');
35+
}

0 commit comments

Comments
 (0)