Skip to content

Commit 138659b

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 <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent c02afe6 commit 138659b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/internal/errors.js

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

154+
let useOriginalName = false;
155+
154156
function makeNodeErrorWithCode(Base, key) {
155157
return class NodeError extends Base {
156158
constructor(...args) {
157159
super(getMessage(key, args));
158160
}
159161

160162
get name() {
163+
if (useOriginalName) {
164+
return super.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+
get useOriginalName() { return useOriginalName; },
452+
set useOriginalName(value) { useOriginalName = value; }
443453
};
444454

445455
// To declare an error message, use the E(sym, val, def) function above. The sym
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+
const assert = require('assert');
12+
const errors = require('internal/errors');
13+
14+
15+
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
16+
Error);
17+
{
18+
const err = new errors.codes.TEST_ERROR_1('test');
19+
assert(err instanceof Error);
20+
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
21+
}
22+
23+
{
24+
errors.useOriginalName = true;
25+
const err = new errors.codes.TEST_ERROR_1('test');
26+
assert(err instanceof Error);
27+
assert.strictEqual(err.name, 'Error');
28+
}
29+
30+
{
31+
errors.useOriginalName = false;
32+
const err = new errors.codes.TEST_ERROR_1('test');
33+
assert(err instanceof Error);
34+
assert.strictEqual(err.name, 'Error [TEST_ERROR_1]');
35+
}

0 commit comments

Comments
 (0)