Skip to content

Commit 058e882

Browse files
Mesteerydanielleadams
authored andcommitted
lib: use ERR_ILLEGAL_CONSTRUCTOR
Use ERR_ILLEGAL_CONSTRUCTOR error instead of `illegal constructor` or `Illegal constructor` TypeError. PR-URL: #39556 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent cff2aea commit 058e882

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
lines changed

lib/internal/abort_controller.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
ObjectDefineProperty,
1111
Symbol,
1212
SymbolToStringTag,
13-
TypeError,
1413
} = primordials;
1514

1615
const {
@@ -25,6 +24,7 @@ const {
2524
const { inspect } = require('internal/util/inspect');
2625
const {
2726
codes: {
27+
ERR_ILLEGAL_CONSTRUCTOR,
2828
ERR_INVALID_THIS,
2929
}
3030
} = require('internal/errors');
@@ -49,8 +49,7 @@ function validateAbortSignal(obj) {
4949

5050
class AbortSignal extends EventTarget {
5151
constructor() {
52-
// eslint-disable-next-line no-restricted-syntax
53-
throw new TypeError('Illegal constructor');
52+
throw new ERR_ILLEGAL_CONSTRUCTOR();
5453
}
5554

5655
get aborted() {

lib/internal/crypto/keys.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ const {
3434
codes: {
3535
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
3636
ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE,
37+
ERR_CRYPTO_INVALID_JWK,
38+
ERR_ILLEGAL_CONSTRUCTOR,
3739
ERR_INVALID_ARG_TYPE,
3840
ERR_INVALID_ARG_VALUE,
3941
ERR_OUT_OF_RANGE,
40-
ERR_OPERATION_FAILED,
41-
ERR_CRYPTO_INVALID_JWK,
4242
}
4343
} = require('internal/errors');
4444

@@ -631,7 +631,7 @@ function isKeyObject(obj) {
631631
// would be fantastic if we could find a way of making those interop.
632632
class CryptoKey extends JSTransferable {
633633
constructor() {
634-
throw new ERR_OPERATION_FAILED('Illegal constructor');
634+
throw new ERR_ILLEGAL_CONSTRUCTOR();
635635
}
636636

637637
[kInspect](depth, options) {

lib/internal/histogram.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ObjectSetPrototypeOf,
88
SafeMap,
99
Symbol,
10-
TypeError,
1110
} = primordials;
1211

1312
const {
@@ -22,6 +21,7 @@ const { inspect } = require('util');
2221

2322
const {
2423
codes: {
24+
ERR_ILLEGAL_CONSTRUCTOR,
2525
ERR_INVALID_ARG_VALUE,
2626
ERR_INVALID_ARG_TYPE,
2727
ERR_OUT_OF_RANGE,
@@ -130,8 +130,7 @@ class Histogram extends JSTransferable {
130130

131131
class RecordableHistogram extends Histogram {
132132
constructor() {
133-
// eslint-disable-next-line no-restricted-syntax
134-
throw new TypeError('illegal constructor');
133+
throw new ERR_ILLEGAL_CONSTRUCTOR();
135134
}
136135

137136
record(val) {

lib/internal/perf/event_loop_delay.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22
const {
33
Symbol,
4-
TypeError,
54
} = primordials;
65

6+
const {
7+
codes: {
8+
ERR_ILLEGAL_CONSTRUCTOR,
9+
}
10+
} = require('internal/errors');
11+
712
const {
813
ELDHistogram: _ELDHistogram,
914
} = internalBinding('performance');
@@ -23,8 +28,7 @@ const kEnabled = Symbol('kEnabled');
2328
class ELDHistogram extends Histogram {
2429
constructor(i) {
2530
if (!(i instanceof _ELDHistogram)) {
26-
// eslint-disable-next-line no-restricted-syntax
27-
throw new TypeError('illegal constructor');
31+
throw new ERR_ILLEGAL_CONSTRUCTOR();
2832
}
2933
super(i);
3034
this[kEnabled] = false;

lib/internal/perf/performance.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ const {
44
ObjectDefineProperty,
55
ObjectDefineProperties,
66
ObjectSetPrototypeOf,
7-
TypeError,
87
} = primordials;
98

9+
const {
10+
codes: {
11+
ERR_ILLEGAL_CONSTRUCTOR,
12+
}
13+
} = require('internal/errors');
14+
1015
const {
1116
EventTarget,
1217
} = require('internal/event_target');
@@ -35,8 +40,7 @@ const {
3540

3641
class Performance extends EventTarget {
3742
constructor() {
38-
// eslint-disable-next-line no-restricted-syntax
39-
throw new TypeError('Illegal constructor');
43+
throw new ERR_ILLEGAL_CONSTRUCTOR();
4044
}
4145

4246
[kInspect](depth, options) {

lib/internal/perf/performance_entry.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
const {
44
ObjectSetPrototypeOf,
55
Symbol,
6-
TypeError,
76
} = primordials;
87

8+
const {
9+
codes: {
10+
ERR_ILLEGAL_CONSTRUCTOR,
11+
}
12+
} = require('internal/errors');
13+
914
const {
1015
customInspectSymbol: kInspect,
1116
} = require('internal/util');
@@ -25,8 +30,7 @@ function isPerformanceEntry(obj) {
2530

2631
class PerformanceEntry {
2732
constructor() {
28-
// eslint-disable-next-line no-restricted-syntax
29-
throw new TypeError('illegal constructor');
33+
throw new ERR_ILLEGAL_CONSTRUCTOR();
3034
}
3135

3236
get name() { return this[kName]; }

test/parallel/test-abortcontroller.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ const { ok, strictEqual, throws } = require('assert');
5656
{
5757
// Tests that AbortSignal is impossible to construct manually
5858
const ac = new AbortController();
59-
throws(
60-
() => new ac.signal.constructor(),
61-
/^TypeError: Illegal constructor$/
62-
);
59+
throws(() => new ac.signal.constructor(), {
60+
code: 'ERR_ILLEGAL_CONSTRUCTOR',
61+
});
6362
}
6463
{
6564
// Symbol.toStringTag

test/parallel/test-perf-hooks-histogram.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,5 @@ const { inspect } = require('util');
7878
{
7979
// Tests that RecordableHistogram is impossible to construct manually
8080
const h = createHistogram();
81-
assert.throws(
82-
() => new h.constructor(),
83-
/^TypeError: illegal constructor$/
84-
);
81+
assert.throws(() => new h.constructor(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
8582
}

test/parallel/test-webcrypto-keygen.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,7 @@ const vectors = {
657657
})().then(common.mustCall());
658658

659659
// End user code cannot create CryptoKey directly
660-
assert.throws(() => new CryptoKey(), {
661-
code: 'ERR_OPERATION_FAILED'
662-
});
660+
assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
663661

664662
{
665663
const buffer = Buffer.from('Hello World');

0 commit comments

Comments
 (0)