From: Jérôme Benoit Date: Fri, 16 Sep 2022 17:38:06 +0000 (+0200) Subject: Log errors in worker X-Git-Tag: v1.1.74~31 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0f187001df8a7a1ecf7f54b67e9a24bd95c6b444;p=e-mobility-charging-stations-simulator.git Log errors in worker Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index 73c2cb7f..4f109bf5 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -49,11 +49,3 @@ function startChargingStation(data: ChargingStationWorkerData): void { const station = new ChargingStation(data.index, data.templateFile); station.start(); } - -process.on('uncaughtException', (err) => { - throw err; -}); - -process.on('unhandledRejection', (reason) => { - throw reason; -}); diff --git a/src/worker/WorkerDynamicPool.ts b/src/worker/WorkerDynamicPool.ts index b2cd20dd..68ba56e9 100644 --- a/src/worker/WorkerDynamicPool.ts +++ b/src/worker/WorkerDynamicPool.ts @@ -16,6 +16,8 @@ export default class WorkerDynamicPool extends WorkerAbstract { */ constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); + this.workerOptions.poolOptions.errorHandler = + this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler; this.workerOptions.poolOptions.exitHandler = this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; this.pool = new DynamicThreadPool( diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 956d97de..2fed62f5 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -96,8 +96,8 @@ export default class WorkerSet extends WorkerAbstract { /* This is intentional */ }); }); - worker.on('error', () => { - /* This is intentional */ + worker.on('error', (error) => { + WorkerUtils.defaultErrorHandler(error); }); worker.on('exit', (code) => { WorkerUtils.defaultExitHandler(code); diff --git a/src/worker/WorkerStaticPool.ts b/src/worker/WorkerStaticPool.ts index 6fc91524..23112a6c 100644 --- a/src/worker/WorkerStaticPool.ts +++ b/src/worker/WorkerStaticPool.ts @@ -16,6 +16,8 @@ export default class WorkerStaticPool extends WorkerAbstract { */ constructor(workerScript: string, workerOptions?: WorkerOptions) { super(workerScript, workerOptions); + this.workerOptions.poolOptions.errorHandler = + this.workerOptions?.poolOptions?.errorHandler ?? WorkerUtils.defaultErrorHandler; this.workerOptions.poolOptions.exitHandler = this.workerOptions?.poolOptions?.exitHandler ?? WorkerUtils.defaultExitHandler; this.pool = new FixedThreadPool( diff --git a/src/worker/WorkerUtils.ts b/src/worker/WorkerUtils.ts index 00feba44..4d6868f7 100644 --- a/src/worker/WorkerUtils.ts +++ b/src/worker/WorkerUtils.ts @@ -10,4 +10,8 @@ export class WorkerUtils { console.error(chalk.red(`Worker stopped with exit code ${code}`)); } }; + + public static defaultErrorHandler = (error: Error): void => { + console.error(chalk.red('Worker errored: ', error)); + }; }