Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d69d06b

Browse filesBrowse 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
Copy full SHA for d69d06b

File tree

Expand file treeCollapse file tree

2 files changed

+46
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+46
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+11-1Lines changed: 11 additions & 1 deletion
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
Collapse file
+35Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.