fix: ensure WorkerError similarity between cluster and worker-threads
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 25 Oct 2024 14:58:28 +0000 (16:58 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 25 Oct 2024 14:58:28 +0000 (16:58 +0200)
pool

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/utility-types.ts
src/worker/abstract-worker.ts
src/worker/thread-worker.ts
tests/pools/cluster/fixed.test.mjs
tests/pools/thread/fixed.test.mjs
tests/worker/thread-worker.test.mjs

index ae4e3f10b027cab3c34e5b5798a05042c37d9126..ce1ef3a3002ec13e03934056a735dc9eeeca6f87 100644 (file)
@@ -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
                   }'`
                 )
index c5caefc1dd00a4ff5556c3d2fc449bda9bc071b6..fdc3a0568c4fcabd0dc5cf445bd842d94eb3f8b2 100644 (file)
@@ -21,7 +21,7 @@ export interface WorkerError<Data = unknown> {
   /**
    * Error message.
    */
-  readonly message?: string
+  readonly message: string
   /**
    * Task function name triggering the error.
    */
index 48f14b071724b7bafe7fd9e17115fec41bade3df..0b425f7895b8a181cbab76c574202983f681c4a8 100644 (file)
@@ -225,7 +225,7 @@ export abstract class AbstractWorker<
    */
   protected abstract handleError (error: Error): {
     error?: Error
-    message?: string
+    message: string
     stack?: string
   }
 
index 4204d565aa033bd42b9d7303306d9ad11c449365..513acbd5fe55ac8bdab9ce6fb73908bb8725d674 100644 (file)
@@ -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 */
index 748e9b2b26b340ac8b6b2c7be0d2e9a67c250850..4be196d6077aa2b9a0156418a855f9750fd48f5f 100644 (file)
@@ -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(
index 8086bcfcaeee9d255b21ff5cd38b5edb520a0094..734ca0020461277053999bbf95cb30d504356c1d 100644 (file)
@@ -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(
index 2dbc9cb0baf609e1a21410a9c1b06789fda2c64d..269f4a7aa58adfe28d51e9bd773d8adf3bd915bd 100644 (file)
@@ -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', () => {