X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FBootstrap.ts;h=7c80067ccd93905b2e76cec7ac9a3641bd670ff6;hb=36999e7745b090d38c68015929b5d223b4162613;hp=37cd1e64d2693ae99436137aebe32df5a7cfe46e;hpb=e2c77f1010574abfb90423c9e593c09d6e447c96;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 37cd1e64..7c80067c 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -18,7 +18,7 @@ import { ChargingStationWorkerMessageEvents, } from '../types/ChargingStationWorker'; import type { StationTemplateUrl } from '../types/ConfigurationData'; -import type Statistics from '../types/Statistics'; +import type { Statistics } from '../types/Statistics'; import Configuration from '../utils/Configuration'; import logger from '../utils/Logger'; import Utils from '../utils/Utils'; @@ -35,7 +35,7 @@ const noChargingStationTemplatesExitCode = 2; export class Bootstrap { private static instance: Bootstrap | null = null; - private workerImplementation: WorkerAbstract | null = null; + private workerImplementation: WorkerAbstract | null; private readonly uiServer!: AbstractUIServer; private readonly storage!: Storage; private numberOfChargingStationTemplates!: number; @@ -47,6 +47,7 @@ export class Bootstrap { private constructor() { this.started = false; + this.workerImplementation = null; this.workerScript = path.join( path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../'), 'charging-station', @@ -74,6 +75,9 @@ export class Bootstrap { public async start(): Promise { if (isMainThread && this.started === false) { try { + // Enable unconditionally for now + this.logUnhandledRejection(); + this.logUncaughtException(); this.initialize(); await this.storage?.open(); await this.workerImplementation.start(); @@ -146,10 +150,10 @@ export class Bootstrap { this.workerImplementation = null; this.uiServer?.stop(); await this.storage?.close(); + this.started = false; } else { - console.error(chalk.red('Trying to stop the charging stations simulator while not started')); + console.error(chalk.red('Cannot stop a not started charging stations simulator')); } - this.started = false; } public async restart(): Promise { @@ -243,6 +247,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