From 6f82891836b8086f8239430dc25abb364fa53e0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 25 Oct 2024 17:21:56 +0200 Subject: [PATCH] refactor: factor out WorkerError handling 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 | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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) { -- 2.34.1