fix: ensure task error proper throw with worker-threads
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 15:13:05 +0000 (17:13 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 15:13:05 +0000 (17:13 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/worker/abstract-worker.ts
src/worker/cluster-worker.ts
src/worker/thread-worker.ts
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index f71bf2008c6fa955aea588e7b2c5ab905e690f47..52a2aa383da0dd89acf4b3db7e518fd18fc33f21 100644 (file)
@@ -832,9 +832,7 @@ export abstract class AbstractPool<
             if (this.emitter != null) {
               this.emitter.emit(PoolEvents.taskError, message.taskError)
             }
-            promiseResponse.reject(
-              `${message.taskError.message} on worker '${message.taskError.workerId}'`
-            )
+            promiseResponse.reject(message.taskError.message)
           } else {
             promiseResponse.resolve(message.data as Response)
           }
index 63d14b2ed2dbd2a592451a7d8363564f8d6c9fbe..f291c52428fee35bd384a845945d64de8ee539e3 100644 (file)
@@ -209,7 +209,7 @@ export abstract class AbstractWorker<
    * @returns The error message.
    */
   protected handleError (e: Error | string): string {
-    return e as string
+    return e instanceof Error ? e.message : e
   }
 
   /**
@@ -233,11 +233,11 @@ export abstract class AbstractWorker<
         id: message.id
       })
     } catch (e) {
-      const err = this.handleError(e as Error)
+      const errorMessage = this.handleError(e as Error | string)
       this.sendToMainWorker({
         taskError: {
           workerId: this.id,
-          message: err,
+          message: errorMessage,
           data: message.data
         },
         id: message.id
@@ -270,11 +270,11 @@ export abstract class AbstractWorker<
         return null
       })
       .catch(e => {
-        const err = this.handleError(e as Error)
+        const errorMessage = this.handleError(e as Error | string)
         this.sendToMainWorker({
           taskError: {
             workerId: this.id,
-            message: err,
+            message: errorMessage,
             data: message.data
           },
           id: message.id
index 180a5273f464cfee65f5a4b0d6d4f1a8032200fc..c43e7f75a7586d2b2a9fa6633eea7da6e44697bf 100644 (file)
@@ -52,9 +52,4 @@ export class ClusterWorker<
   protected sendToMainWorker (message: MessageValue<Response>): void {
     this.getMainWorker().send(message)
   }
-
-  /** @inheritDoc */
-  protected handleError (e: Error | string): string {
-    return e instanceof Error ? e.message : e
-  }
 }
index 2b3df58361eb28a9d0ccc2f694208bb0e4426c02..09135afffc913dd7b983b7cbac9134bb5573b3b3 100644 (file)
@@ -56,4 +56,9 @@ export class ThreadWorker<
   protected sendToMainWorker (message: MessageValue<Response>): void {
     this.getMainWorker().postMessage(message)
   }
+
+  /** @inheritDoc */
+  protected handleError (e: Error | string): string {
+    return e as string
+  }
 }
index 60dac4017c72781b6b1e33a78169910438e3030a..ccdd287046017ab11b3dace1208543bfa4ad818a 100644 (file)
@@ -145,8 +145,12 @@ describe('Fixed cluster pool test suite', () => {
     }
     expect(inError).toBeDefined()
     expect(typeof inError === 'string').toBe(true)
-    expect(inError).toContain('Error Message from ClusterWorker on worker')
-    expect(taskError.data).toStrictEqual(data)
+    expect(inError).toBe('Error Message from ClusterWorker')
+    expect(taskError).toStrictEqual({
+      workerId: expect.any(Number),
+      message: 'Error Message from ClusterWorker',
+      data
+    })
     expect(
       errorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
@@ -168,10 +172,12 @@ describe('Fixed cluster pool test suite', () => {
     }
     expect(inError).toBeDefined()
     expect(typeof inError === 'string').toBe(true)
-    expect(inError).toContain(
-      'Error Message from ClusterWorker:async on worker'
-    )
-    expect(taskError.data).toStrictEqual(data)
+    expect(inError).toBe('Error Message from ClusterWorker:async')
+    expect(taskError).toStrictEqual({
+      workerId: expect.any(Number),
+      message: 'Error Message from ClusterWorker:async',
+      data
+    })
     expect(
       asyncErrorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
index f044895335af397c1599faeee10f6e90d63dcf42..d0e31babb38efb3f15057b0e703a403f062776e8 100644 (file)
@@ -147,10 +147,12 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
-    expect(inError.message).toContain(
-      'Error Message from ThreadWorker on worker'
-    )
-    expect(taskError.data).toStrictEqual(data)
+    expect(inError.message).toBe('Error Message from ThreadWorker')
+    expect(taskError).toStrictEqual({
+      workerId: expect.any(Number),
+      message: new Error('Error Message from ThreadWorker'),
+      data
+    })
     expect(
       errorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1
@@ -174,10 +176,12 @@ describe('Fixed thread pool test suite', () => {
     expect(inError).toBeInstanceOf(Error)
     expect(inError.message).toBeDefined()
     expect(typeof inError.message === 'string').toBe(true)
-    expect(inError.message).toContain(
-      'Error Message from ThreadWorker:async on worker'
-    )
-    expect(taskError.data).toStrictEqual(data)
+    expect(inError.message).toBe('Error Message from ThreadWorker:async')
+    expect(taskError).toStrictEqual({
+      workerId: expect.any(Number),
+      message: new Error('Error Message from ThreadWorker:async'),
+      data
+    })
     expect(
       asyncErrorPool.workerNodes.some(
         workerNode => workerNode.usage.tasks.failed === 1