]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix: improve configuration file validation and error handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 11 Jan 2026 18:36:03 +0000 (19:36 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 11 Jan 2026 18:36:03 +0000 (19:36 +0100)
- Add validation for null/empty configurations after parsing
- Add validation for missing configurationHash before caching
- Add fallback to cached config on file read errors with warning
- Remove non-null assertion on configurationHash assignment

src/charging-station/ChargingStation.ts

index c729d2aeafc295aa2b6b62547c6e0b9854ed2a44..18083c609813c4c25edc73edb5f0a6b79159de0a 100644 (file)
@@ -1206,7 +1206,10 @@ export class ChargingStation extends EventEmitter {
     let configuration: ChargingStationConfiguration | undefined
     if (isNotEmptyString(this.configurationFile) && existsSync(this.configurationFile)) {
       try {
-        if (this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)) {
+        if (
+          isNotEmptyString(this.configurationFileHash) &&
+          this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)
+        ) {
           configuration = this.sharedLRUCache.getChargingStationConfiguration(
             this.configurationFileHash
           )
@@ -1217,9 +1220,25 @@ export class ChargingStation extends EventEmitter {
             readFileSync(this.configurationFile, 'utf8')
           ) as ChargingStationConfiguration
           PerformanceStatistics.endMeasure(measureId, beginId)
+          // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+          if (configuration == null || isEmpty(configuration)) {
+            logger.error(
+              `${this.logPrefix()} Invalid charging station configuration file ${
+                this.configurationFile
+              }`
+            )
+            return undefined
+          }
+          if (!isNotEmptyString(configuration.configurationHash)) {
+            logger.error(
+              `${this.logPrefix()} Missing charging station configuration hash in file ${
+                this.configurationFile
+              }`
+            )
+            return undefined
+          }
+          this.configurationFileHash = configuration.configurationHash
           this.sharedLRUCache.setChargingStationConfiguration(configuration)
-          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          this.configurationFileHash = configuration.configurationHash!
         }
       } catch (error) {
         handleFileException(
@@ -1228,6 +1247,15 @@ export class ChargingStation extends EventEmitter {
           error as NodeJS.ErrnoException,
           this.logPrefix()
         )
+        if (
+          isNotEmptyString(this.configurationFileHash) &&
+          this.sharedLRUCache.hasChargingStationConfiguration(this.configurationFileHash)
+        ) {
+          logger.warn(
+            `${this.logPrefix()} Using cached charging station configuration due to file read error`
+          )
+          return this.sharedLRUCache.getChargingStationConfiguration(this.configurationFileHash)
+        }
       }
     }
     return configuration