Bump vm2 from 3.9.10 to 3.9.11 (#192)
[e-mobility-charging-stations-simulator.git] / src / charging-station / Bootstrap.ts
index 85f8f371cf64ade08d7e40bd688cd972970a527e..cfb4eed6b309721fc5055998ca71526bc3366cb6 100644 (file)
@@ -2,7 +2,7 @@
 
 import path from 'path';
 import { fileURLToPath } from 'url';
-import { isMainThread } from 'worker_threads';
+import { type Worker, isMainThread } from 'worker_threads';
 
 import chalk from 'chalk';
 
@@ -75,6 +75,9 @@ export class Bootstrap {
   public async start(): Promise<void> {
     if (isMainThread && this.started === false) {
       try {
+        // Enable unconditionally for now
+        this.logUnhandledRejection();
+        this.logUncaughtException();
         this.initialize();
         await this.storage?.open();
         await this.workerImplementation.start();
@@ -174,6 +177,7 @@ export class Bootstrap {
             workerChoiceStrategy: Configuration.getWorker().poolStrategy,
           },
           messageHandler: this.messageHandler.bind(this) as (
+            this: Worker,
             msg: ChargingStationWorkerMessage<ChargingStationWorkerMessageData>
           ) => void,
         }
@@ -244,6 +248,18 @@ export class Bootstrap {
     this.initializeWorkerImplementation();
   }
 
+  private logUncaughtException(): void {
+    process.on('uncaughtException', (error: Error) => {
+      console.error(chalk.red('Uncaught exception: '), error);
+    });
+  }
+
+  private logUnhandledRejection(): void {
+    process.on('unhandledRejection', (reason: unknown) => {
+      console.error(chalk.red('Unhandled rejection: '), reason);
+    });
+  }
+
   private async startChargingStation(
     index: number,
     stationTemplateUrl: StationTemplateUrl