From 058e882a2a09b436586e1ae2a2a81f27e1bb61f6 Mon Sep 17 00:00:00 2001 From: Mestery Date: Wed, 28 Jul 2021 03:41:18 +0200 Subject: [PATCH] lib: use ERR_ILLEGAL_CONSTRUCTOR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use ERR_ILLEGAL_CONSTRUCTOR error instead of `illegal constructor` or `Illegal constructor` TypeError. PR-URL: https://github.com/nodejs/node/pull/39556 Reviewed-By: Michaƫl Zasso Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen --- lib/internal/abort_controller.js | 5 ++--- lib/internal/crypto/keys.js | 6 +++--- lib/internal/histogram.js | 5 ++--- lib/internal/perf/event_loop_delay.js | 10 +++++++--- lib/internal/perf/performance.js | 10 +++++++--- lib/internal/perf/performance_entry.js | 10 +++++++--- test/parallel/test-abortcontroller.js | 7 +++---- test/parallel/test-perf-hooks-histogram.js | 5 +---- test/parallel/test-webcrypto-keygen.js | 4 +--- 9 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index e6ee07052617d5..c24963b823c683 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -10,7 +10,6 @@ const { ObjectDefineProperty, Symbol, SymbolToStringTag, - TypeError, } = primordials; const { @@ -25,6 +24,7 @@ const { const { inspect } = require('internal/util/inspect'); const { codes: { + ERR_ILLEGAL_CONSTRUCTOR, ERR_INVALID_THIS, } } = require('internal/errors'); @@ -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() { diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 27e28942fdaa8a..1127217d6b9dee 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -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'); @@ -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) { diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index 2df321300fb87e..f437bfd4d791ac 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -7,7 +7,6 @@ const { ObjectSetPrototypeOf, SafeMap, Symbol, - TypeError, } = primordials; const { @@ -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, @@ -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) { diff --git a/lib/internal/perf/event_loop_delay.js b/lib/internal/perf/event_loop_delay.js index f90bb9e4de7d58..f5d0eb74d588e4 100644 --- a/lib/internal/perf/event_loop_delay.js +++ b/lib/internal/perf/event_loop_delay.js @@ -1,9 +1,14 @@ 'use strict'; const { Symbol, - TypeError, } = primordials; +const { + codes: { + ERR_ILLEGAL_CONSTRUCTOR, + } +} = require('internal/errors'); + const { ELDHistogram: _ELDHistogram, } = internalBinding('performance'); @@ -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; diff --git a/lib/internal/perf/performance.js b/lib/internal/perf/performance.js index 2f75eb143a6ffe..38dac0ee32397c 100644 --- a/lib/internal/perf/performance.js +++ b/lib/internal/perf/performance.js @@ -4,9 +4,14 @@ const { ObjectDefineProperty, ObjectDefineProperties, ObjectSetPrototypeOf, - TypeError, } = primordials; +const { + codes: { + ERR_ILLEGAL_CONSTRUCTOR, + } +} = require('internal/errors'); + const { EventTarget, } = require('internal/event_target'); @@ -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) { diff --git a/lib/internal/perf/performance_entry.js b/lib/internal/perf/performance_entry.js index 8fcb0ca3fcdc0c..d8eedb9fb8f85b 100644 --- a/lib/internal/perf/performance_entry.js +++ b/lib/internal/perf/performance_entry.js @@ -3,9 +3,14 @@ const { ObjectSetPrototypeOf, Symbol, - TypeError, } = primordials; +const { + codes: { + ERR_ILLEGAL_CONSTRUCTOR, + } +} = require('internal/errors'); + const { customInspectSymbol: kInspect, } = require('internal/util'); @@ -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]; } diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index f7a70fbddbc89e..630a1c5708d765 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -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 diff --git a/test/parallel/test-perf-hooks-histogram.js b/test/parallel/test-perf-hooks-histogram.js index 323dbeb153633a..a60d3a94bbc136 100644 --- a/test/parallel/test-perf-hooks-histogram.js +++ b/test/parallel/test-perf-hooks-histogram.js @@ -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' }); } diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index e94a7b4488222a..502c86cf32abf3 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -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');