refactor: improve error reporting at task functions handling
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Sep 2023 12:09:43 +0000 (14:09 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 19 Sep 2023 12:09:43 +0000 (14:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
tests/pools/abstract/abstract-pool.test.js

index 33585218249e599cd1998fdc19158e7ab4b22168..9aeeebed772b37234c2a8f0e88c5cb45c65f0dce 100644 (file)
@@ -782,9 +782,11 @@ export abstract class AbstractPool<
         ) {
           reject(
             new Error(
-              `Task function operation ${
+              `Task function operation '${
                 message.taskFunctionOperation as string
-              } failed on worker ${message.workerId}`
+              }' failed on worker ${message.workerId} with error: '${
+                message.workerError?.message as string
+              }'`
             )
           )
         }
@@ -815,11 +817,18 @@ export abstract class AbstractPool<
                 message => message.taskFunctionOperationStatus === false
               )
             ) {
+              const errorResponse = responsesReceived.find(
+                response => response.taskFunctionOperationStatus === false
+              )
               reject(
                 new Error(
-                  `Task function operation ${
+                  `Task function operation '${
                     message.taskFunctionOperation as string
-                  } failed on worker ${message.workerId as number}`
+                  }' failed on worker ${
+                    errorResponse?.workerId as number
+                  } with error: '${
+                    errorResponse?.workerError?.message as string
+                  }'`
                 )
               )
             }
index 3f59396a192efce942e1fc29b2164e16edf719b7..f6a54d5b4473894f42778dbb6ea3abc8536a9f02 100644 (file)
@@ -1420,6 +1420,27 @@ describe('Abstract pool test suite', () => {
       './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js'
     )
     await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    await expect(
+      dynamicThreadPool.setDefaultTaskFunction(0)
+    ).rejects.toThrowError(
+      new Error(
+        "Task function operation 'default' failed on worker 31 with error: 'TypeError: name parameter is not a string'"
+      )
+    )
+    await expect(
+      dynamicThreadPool.setDefaultTaskFunction(DEFAULT_TASK_NAME)
+    ).rejects.toThrowError(
+      new Error(
+        "Task function operation 'default' failed on worker 31 with error: 'Error: Cannot set the default task function reserved name as the default task function'"
+      )
+    )
+    await expect(
+      dynamicThreadPool.setDefaultTaskFunction('unknown')
+    ).rejects.toThrowError(
+      new Error(
+        "Task function operation 'default' failed on worker 31 with error: 'Error: Cannot set the default task function to a non-existing task function'"
+      )
+    )
     expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([
       DEFAULT_TASK_NAME,
       'jsonIntegerSerialization',