From e695d66fae7e1343a140fee2eaecb5069432fc36 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 19 Aug 2023 23:20:04 +0200 Subject: [PATCH] refactor: cleanup error type MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 2 +- src/pools/abstract-pool.ts | 4 ++-- .../worker-choice-strategy-context.ts | 2 +- src/pools/worker-node.ts | 10 ++++++---- tests/pools/abstract/abstract-pool.test.js | 14 +++++++++----- .../worker-choice-strategy-context.test.js | 8 ++------ 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec60d67a..80439e06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Make orthogonal worker choice strategies tasks distribution and dynamic worker creation usage. +- Make orthogonal worker choice strategies tasks distribution and created dynamic worker usage. - Remove the experimental status of the `LEAST_ELU` worker choice strategy. ## [2.6.30] - 2023-08-19 diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 1aa1a134..289d6b35 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -179,7 +179,7 @@ export abstract class AbstractPool< protected checkDynamicPoolSize (min: number, max: number): void { if (this.type === PoolTypes.dynamic) { if (max == null) { - throw new Error( + throw new TypeError( 'Cannot instantiate a dynamic pool without specifying the maximum pool size' ) } else if (!Number.isSafeInteger(max)) { @@ -302,7 +302,7 @@ export abstract class AbstractPool< tasksQueueOptions?.concurrency != null && tasksQueueOptions.concurrency <= 0 ) { - throw new Error( + throw new RangeError( `Invalid worker tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero` ) } diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index a3e3379c..51de6e7b 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -183,7 +183,7 @@ export class WorkerChoiceStrategyContext< this.choiceRetriesCount++ return this.execute() } else if (workerNodeKey == null) { - throw new TypeError( + throw new Error( `Worker node key chosen is null or undefined after ${this.choiceRetriesCount} retries` ) } diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 09e4a7b5..57628bf7 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -41,18 +41,20 @@ implements IWorkerNode { */ constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) { if (worker == null) { - throw new Error('Cannot construct a worker node without a worker') + throw new TypeError('Cannot construct a worker node without a worker') } if (workerType == null) { - throw new Error('Cannot construct a worker node without a worker type') + throw new TypeError( + 'Cannot construct a worker node without a worker type' + ) } if (poolMaxSize == null) { - throw new Error( + throw new TypeError( 'Cannot construct a worker node without a pool maximum size' ) } if (isNaN(poolMaxSize)) { - throw new Error( + throw new TypeError( 'Cannot construct a worker node with a NaN pool maximum size' ) } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index d1c58ddb..867c1415 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -34,7 +34,9 @@ describe('Abstract pool test suite', () => { } ) ).toThrowError( - 'Cannot start a pool from a worker with the same type as the pool' + new Error( + 'Cannot start a pool from a worker with the same type as the pool' + ) ) }) @@ -61,7 +63,9 @@ describe('Abstract pool test suite', () => { it('Verify that numberOfWorkers is checked', () => { expect(() => new FixedThreadPool()).toThrowError( - 'Cannot instantiate a pool without specifying the number of workers' + new Error( + 'Cannot instantiate a pool without specifying the number of workers' + ) ) }) @@ -285,7 +289,7 @@ describe('Abstract pool test suite', () => { } ) ).toThrowError( - new TypeError( + new RangeError( 'Invalid worker tasks concurrency: 0 is a negative integer or zero' ) ) @@ -509,12 +513,12 @@ describe('Abstract pool test suite', () => { new TypeError('Invalid tasks queue options: must be a plain object') ) expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( - new Error( + new RangeError( 'Invalid worker tasks concurrency: 0 is a negative integer or zero' ) ) expect(() => pool.setTasksQueueOptions({ concurrency: -1 })).toThrowError( - new Error( + new RangeError( 'Invalid worker tasks concurrency: -1 is a negative integer or zero' ) ) diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index 63e58d57..df8cd25c 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -115,18 +115,14 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategyUndefinedStub ) expect(() => workerChoiceStrategyContext.execute()).toThrowError( - new TypeError( - 'Worker node key chosen is null or undefined after 6 retries' - ) + new Error('Worker node key chosen is null or undefined after 6 retries') ) workerChoiceStrategyContext.workerChoiceStrategies.set( workerChoiceStrategyContext.workerChoiceStrategy, WorkerChoiceStrategyNullStub ) expect(() => workerChoiceStrategyContext.execute()).toThrowError( - new TypeError( - 'Worker node key chosen is null or undefined after 6 retries' - ) + new Error('Worker node key chosen is null or undefined after 6 retries') ) }) -- 2.34.1