Skip to content

Commit

Permalink
lib: use ERR_ILLEGAL_CONSTRUCTOR
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Mesteery authored and danielleadams committed Aug 16, 2021
1 parent cff2aea commit 058e882
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 29 deletions.
5 changes: 2 additions & 3 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
ObjectDefineProperty,
Symbol,
SymbolToStringTag,
TypeError,
} = primordials;

const {
Expand All @@ -25,6 +24,7 @@ const {
const { inspect } = require('internal/util/inspect');
const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_THIS,
}
} = require('internal/errors');
Expand All @@ -49,8 +49,7 @@ function validateAbortSignal(obj) {

class AbortSignal extends EventTarget {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('Illegal constructor');
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

get aborted() {
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const {
codes: {
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE,
ERR_CRYPTO_INVALID_JWK,
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
ERR_OPERATION_FAILED,
ERR_CRYPTO_INVALID_JWK,
}
} = require('internal/errors');

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

[kInspect](depth, options) {
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
ObjectSetPrototypeOf,
SafeMap,
Symbol,
TypeError,
} = primordials;

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

const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
Expand Down Expand Up @@ -130,8 +130,7 @@ class Histogram extends JSTransferable {

class RecordableHistogram extends Histogram {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('illegal constructor');
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

record(val) {
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/perf/event_loop_delay.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';
const {
Symbol,
TypeError,
} = primordials;

const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
}
} = require('internal/errors');

const {
ELDHistogram: _ELDHistogram,
} = internalBinding('performance');
Expand All @@ -23,8 +28,7 @@ const kEnabled = Symbol('kEnabled');
class ELDHistogram extends Histogram {
constructor(i) {
if (!(i instanceof _ELDHistogram)) {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('illegal constructor');
throw new ERR_ILLEGAL_CONSTRUCTOR();
}
super(i);
this[kEnabled] = false;
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/perf/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ const {
ObjectDefineProperty,
ObjectDefineProperties,
ObjectSetPrototypeOf,
TypeError,
} = primordials;

const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
}
} = require('internal/errors');

const {
EventTarget,
} = require('internal/event_target');
Expand Down Expand Up @@ -35,8 +40,7 @@ const {

class Performance extends EventTarget {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('Illegal constructor');
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

[kInspect](depth, options) {
Expand Down
10 changes: 7 additions & 3 deletions lib/internal/perf/performance_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
const {
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;

const {
codes: {
ERR_ILLEGAL_CONSTRUCTOR,
}
} = require('internal/errors');

const {
customInspectSymbol: kInspect,
} = require('internal/util');
Expand All @@ -25,8 +30,7 @@ function isPerformanceEntry(obj) {

class PerformanceEntry {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('illegal constructor');
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

get name() { return this[kName]; }
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ const { ok, strictEqual, throws } = require('assert');
{
// Tests that AbortSignal is impossible to construct manually
const ac = new AbortController();
throws(
() => new ac.signal.constructor(),
/^TypeError: Illegal constructor$/
);
throws(() => new ac.signal.constructor(), {
code: 'ERR_ILLEGAL_CONSTRUCTOR',
});
}
{
// Symbol.toStringTag
Expand Down
5 changes: 1 addition & 4 deletions test/parallel/test-perf-hooks-histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,5 @@ const { inspect } = require('util');
{
// Tests that RecordableHistogram is impossible to construct manually
const h = createHistogram();
assert.throws(
() => new h.constructor(),
/^TypeError: illegal constructor$/
);
assert.throws(() => new h.constructor(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });
}
4 changes: 1 addition & 3 deletions test/parallel/test-webcrypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,7 @@ const vectors = {
})().then(common.mustCall());

// End user code cannot create CryptoKey directly
assert.throws(() => new CryptoKey(), {
code: 'ERR_OPERATION_FAILED'
});
assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' });

{
const buffer = Buffer.from('Hello World');
Expand Down

0 comments on commit 058e882

Please sign in to comment.