From: Jérôme Benoit Date: Fri, 25 Oct 2024 14:58:28 +0000 (+0200) Subject: fix: ensure WorkerError similarity between cluster and worker-threads X-Git-Tag: v4.4.2~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1b12ff49353f2ba5eadd8fc0241895b050cbc3d4;p=poolifier.git fix: ensure WorkerError similarity between cluster and worker-threads pool Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ae4e3f10..ce1ef3a3 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1497,7 +1497,6 @@ export abstract class AbstractPool< // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Task function operation '${message.taskFunctionOperation?.toString()}' failed on worker ${message.workerId?.toString()} with error: '${ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - message.workerError?.error?.message ?? message.workerError?.message }'` ) @@ -1550,7 +1549,6 @@ export abstract class AbstractPool< // eslint-disable-next-line @typescript-eslint/restrict-template-expressions }' failed on worker ${errorResponse?.workerId?.toString()} with error: '${ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - errorResponse?.workerError?.error?.message ?? errorResponse?.workerError?.message }'` ) diff --git a/src/utility-types.ts b/src/utility-types.ts index c5caefc1..fdc3a056 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -21,7 +21,7 @@ export interface WorkerError { /** * Error message. */ - readonly message?: string + readonly message: string /** * Task function name triggering the error. */ diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 48f14b07..0b425f78 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -225,7 +225,7 @@ export abstract class AbstractWorker< */ protected abstract handleError (error: Error): { error?: Error - message?: string + message: string stack?: string } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 4204d565..513acbd5 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -58,8 +58,12 @@ export class ThreadWorker< /** * @inheritDoc */ - protected handleError (error: Error): { error: Error } { - return { error } + protected handleError (error: Error): { + error: Error + message: string + stack?: string + } { + return { error, message: error.message, stack: error.stack } } /** @inheritDoc */ diff --git a/tests/pools/cluster/fixed.test.mjs b/tests/pools/cluster/fixed.test.mjs index 748e9b2b..4be196d6 100644 --- a/tests/pools/cluster/fixed.test.mjs +++ b/tests/pools/cluster/fixed.test.mjs @@ -181,9 +181,9 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError.stack === 'string').toBe(true) expect(taskError).toStrictEqual({ data, - message: 'Error Message from ClusterWorker', + message: inError.message, name: DEFAULT_TASK_NAME, - stack: expect.any(String), + stack: inError.stack, }) expect( errorPool.workerNodes.some( @@ -215,9 +215,9 @@ describe('Fixed cluster pool test suite', () => { expect(typeof inError.stack === 'string').toBe(true) expect(taskError).toStrictEqual({ data, - message: 'Error Message from ClusterWorker:async', + message: inError.message, name: DEFAULT_TASK_NAME, - stack: expect.any(String), + stack: inError.stack, }) expect( asyncErrorPool.workerNodes.some( diff --git a/tests/pools/thread/fixed.test.mjs b/tests/pools/thread/fixed.test.mjs index 8086bcfc..734ca002 100644 --- a/tests/pools/thread/fixed.test.mjs +++ b/tests/pools/thread/fixed.test.mjs @@ -204,10 +204,13 @@ describe('Fixed thread pool test suite', () => { } expect(inError).toBeInstanceOf(Error) expect(inError.message).toStrictEqual('Error Message from ThreadWorker') + expect(typeof inError.stack === 'string').toBe(true) expect(taskError).toStrictEqual({ data, - error: new Error('Error Message from ThreadWorker'), + error: inError, + message: inError.message, name: DEFAULT_TASK_NAME, + stack: inError.stack, }) expect( errorPool.workerNodes.some( @@ -236,10 +239,13 @@ describe('Fixed thread pool test suite', () => { expect(inError.message).toStrictEqual( 'Error Message from ThreadWorker:async' ) + expect(typeof inError.stack === 'string').toBe(true) expect(taskError).toStrictEqual({ data, - error: new Error('Error Message from ThreadWorker:async'), + error: inError, + message: inError.message, name: DEFAULT_TASK_NAME, + stack: inError.stack, }) expect( asyncErrorPool.workerNodes.some( diff --git a/tests/worker/thread-worker.test.mjs b/tests/worker/thread-worker.test.mjs index 2dbc9cb0..269f4a7a 100644 --- a/tests/worker/thread-worker.test.mjs +++ b/tests/worker/thread-worker.test.mjs @@ -93,7 +93,11 @@ describe('Thread worker test suite', () => { it('Verify that handleError() method is working properly', () => { const error = new Error('Error as an error') const worker = new ThreadWorker(() => {}) - expect(worker.handleError(error)).toStrictEqual({ error }) + expect(worker.handleError(error)).toStrictEqual({ + error, + message: error.message, + stack: error.stack, + }) }) it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => {