refactor: improve error messages
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 10:30:06 +0000 (12:30 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 Jul 2023 10:30:06 +0000 (12:30 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/utility-types.ts
src/worker/abstract-worker.ts

index a963f6bde00d3e1a9b374ea757aad903b83919ff..889390a73c6b4f557afa8c111fab059f69a50737 100644 (file)
@@ -820,7 +820,9 @@ export abstract class AbstractPool<
           this.workerNodes[this.getWorkerNodeKey(worker)].info.started =
             message.started
         } else {
-          throw new Error('Worker started message received from unknown worker')
+          throw new Error(
+            `Worker started message received from unknown worker '${message.workerId}'`
+          )
         }
       } else if (message.id != null) {
         // Task execution response received
@@ -830,7 +832,9 @@ export abstract class AbstractPool<
             if (this.emitter != null) {
               this.emitter.emit(PoolEvents.taskError, message.taskError)
             }
-            promiseResponse.reject(message.taskError.message)
+            promiseResponse.reject(
+              `${message.taskError.message} on worker '${message.taskError.workerId}`
+            )
           } else {
             promiseResponse.resolve(message.data as Response)
           }
@@ -885,7 +889,7 @@ export abstract class AbstractPool<
   private pushWorkerNode (worker: Worker): number {
     this.workerNodes.push({
       worker,
-      info: { id: this.getWorkerId(worker), started: false },
+      info: { id: this.getWorkerId(worker), started: true },
       usage: this.getWorkerUsage(),
       tasksQueue: new Queue<Task<Data>>()
     })
index 0443b4ceef2374e28ba8201bf5d1d6a61ebbce93..bca2de2b91eec91a2c1113e5b422f5cc03301cba 100644 (file)
@@ -8,6 +8,10 @@ import type { IWorker, Task } from './pools/worker'
  * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
  */
 export interface TaskError<Data = unknown> {
+  /**
+   * Worker id.
+   */
+  workerId: number
   /**
    * Error message.
    */
index 6e17c9ed92f53a3612d214f782d38019875ed46c..63d14b2ed2dbd2a592451a7d8363564f8d6c9fbe 100644 (file)
@@ -236,10 +236,10 @@ export abstract class AbstractWorker<
       const err = this.handleError(e as Error)
       this.sendToMainWorker({
         taskError: {
+          workerId: this.id,
           message: err,
           data: message.data
         },
-        workerId: this.id,
         id: message.id
       })
     } finally {
@@ -273,10 +273,10 @@ export abstract class AbstractWorker<
         const err = this.handleError(e as Error)
         this.sendToMainWorker({
           taskError: {
+            workerId: this.id,
             message: err,
             data: message.data
           },
-          workerId: this.id,
           id: message.id
         })
       })