refactor: improve error reporting in the UI server code
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
index 61593db54b1e233cda4b489c39a4d937c518a71f..566d496167d288d0861ad694cd81ee19c61b90f6 100644 (file)
@@ -19,6 +19,9 @@ import {
   type ResponsePayload,
   type UIServerConfiguration
 } from '../../types/index.js'
+import { logger } from '../../utils/index.js'
+
+const moduleName = 'AbstractUIServer'
 
 export abstract class AbstractUIServer {
   public readonly chargingStations: Map<string, ChargingStationData>
@@ -84,6 +87,12 @@ export abstract class AbstractUIServer {
   }
 
   protected startHttpServer (): void {
+    this.httpServer.on('error', error => {
+      logger.error(
+        `${this.logPrefix(moduleName, 'start.httpServer.on.error')} HTTP server error:`,
+        error
+      )
+    })
     if (!this.httpServer.listening) {
       this.httpServer.listen(this.uiServerConfiguration.options)
     }
@@ -116,6 +125,7 @@ export abstract class AbstractUIServer {
   private stopHttpServer (): void {
     if (this.httpServer.listening) {
       this.httpServer.close()
+      this.httpServer.removeAllListeners()
     }
   }