From: Jérôme Benoit Date: Sun, 2 Jul 2023 10:30:06 +0000 (+0200) Subject: refactor: improve error messages X-Git-Tag: v2.6.7~16^2~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7ae6fb74fef4b3a2491d3d56293914fe5243c0d2;p=poolifier.git refactor: improve error messages Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a963f6bd..889390a7 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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>() }) diff --git a/src/utility-types.ts b/src/utility-types.ts index 0443b4ce..bca2de2b 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -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 { + /** + * Worker id. + */ + workerId: number /** * Error message. */ diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 6e17c9ed..63d14b2e 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -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 }) })