Skip to content

Commit e692a09

Browse files
committed
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 ab5f789 commit e692a09

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

@@ -437,7 +442,12 @@ module.exports = {
437442
getMessage,
438443
SystemError,
439444
codes,
440-
E // This is exported only to facilitate testing.
445+
// This is exported only to facilitate testing.
446+
E,
447+
// This allows us to tell the type of the errors without using
448+
// instanceof, which is necessary in WPT harness.
449+
get useOriginalName() { return useOriginalName; },
450+
set useOriginalName(value) { useOriginalName = value; }
441451
};
442452

443453
// 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)