From 7ae6fb74fef4b3a2491d3d56293914fe5243c0d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 2 Jul 2023 12:30:06 +0200 Subject: [PATCH] refactor: improve error messages MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 10 +++++++--- src/utility-types.ts | 4 ++++ src/worker/abstract-worker.ts | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) 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 }) }) -- 2.34.1