From: Jérôme Benoit Date: Sat, 19 Aug 2023 21:20:04 +0000 (+0200) Subject: refactor: cleanup error type X-Git-Tag: v2.6.31~7 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e695d66fae7e1343a140fee2eaecb5069432fc36;p=poolifier.git refactor: cleanup error type Signed-off-by: Jérôme Benoit --- 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') ) })