From: Jérôme Benoit Date: Fri, 25 Oct 2024 15:21:56 +0000 (+0200) Subject: refactor: factor out WorkerError handling X-Git-Tag: v4.4.2~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6f82891836b8086f8239430dc25abb364fa53e0f;p=poolifier.git refactor: factor out WorkerError handling Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ce1ef3a3..77f7a23d 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -10,6 +10,7 @@ import type { PromiseResponseWrapper, Task, TaskFunctionProperties, + WorkerError, } from '../utility-types.js' import type { TaskFunction, @@ -1184,15 +1185,8 @@ export abstract class AbstractPool< const { asyncResource, reject, resolve, workerNodeKey } = promiseResponse const workerNode = this.workerNodes[workerNodeKey] if (workerError != null) { - let error: Error - if (workerError.error != null) { - error = workerError.error - } else { - const err = new Error(workerError.message) - err.stack = workerError.stack - error = err - } this.emitter?.emit(PoolEvents.taskError, workerError) + const error = this.handleWorkerError(workerError) asyncResource != null ? asyncResource.runInAsyncScope(reject, this.emitter, error) : reject(error) @@ -1229,6 +1223,15 @@ export abstract class AbstractPool< } } + private handleWorkerError (workerError: WorkerError): Error { + if (workerError.error != null) { + return workerError.error + } + const error = new Error(workerError.message) + error.stack = workerError.stack + return error + } + private handleWorkerReadyResponse (message: MessageValue): void { const { ready, taskFunctionsProperties, workerId } = message if (ready == null || !ready) {