refactor: refine station information validation error message
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 25 Jul 2024 09:41:44 +0000 (11:41 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 25 Jul 2024 09:41:44 +0000 (11:41 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/Helpers.ts

index 656e77be432a2c3d212d597daa63d36a7671e8d7..225c838f3b5811fb4a4e8115d99538dac8df23b3 100644 (file)
@@ -179,35 +179,37 @@ export const validateStationInfo = (chargingStation: ChargingStation): void => {
   if (isEmpty(chargingStation.stationInfo)) {
     throw new BaseError('Missing charging station information')
   }
+  if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) {
+    throw new BaseError('Missing chargingStationId in stationInfo properties')
+  }
+  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+  const chargingStationId: string = chargingStation.stationInfo!.chargingStationId!
   if (isEmpty(chargingStation.stationInfo?.hashId.trim())) {
-    throw new BaseError('Missing hashId in stationInfo properties')
+    throw new BaseError(`${chargingStationId}: Missing hashId in stationInfo properties`)
   }
   if (isEmpty(chargingStation.stationInfo?.templateIndex)) {
-    throw new BaseError('Missing templateIndex in stationInfo properties')
+    throw new BaseError(`${chargingStationId}: Missing templateIndex in stationInfo properties`)
   }
   if (isEmpty(chargingStation.stationInfo?.templateName.trim())) {
-    throw new BaseError('Missing templateName in stationInfo properties')
-  }
-  if (isEmpty(chargingStation.stationInfo?.chargingStationId?.trim())) {
-    throw new BaseError('Missing chargingStationId in stationInfo properties')
+    throw new BaseError(`${chargingStationId}: Missing templateName in stationInfo properties`)
   }
   if (isEmpty(chargingStation.stationInfo?.maximumPower)) {
-    throw new BaseError('Missing maximumPower in stationInfo properties')
+    throw new BaseError(`${chargingStationId}: Missing maximumPower in stationInfo properties`)
   }
   if (chargingStation.stationInfo?.maximumPower != null && chargingStation.stationInfo.maximumPower <= 0) {
-    throw new RangeError('Invalid maximumPower value in stationInfo properties')
+    throw new RangeError(`${chargingStationId}: Invalid maximumPower value in stationInfo properties`)
   }
   if (isEmpty(chargingStation.stationInfo?.maximumAmperage)) {
-    throw new BaseError('Missing maximumAmperage in stationInfo properties')
+    throw new BaseError(`${chargingStationId}: Missing maximumAmperage in stationInfo properties`)
   }
   if (chargingStation.stationInfo?.maximumAmperage != null && chargingStation.stationInfo.maximumAmperage <= 0) {
-    throw new RangeError('Invalid maximumAmperage value in stationInfo properties')
+    throw new RangeError(`${chargingStationId}: Invalid maximumAmperage value in stationInfo properties`)
   }
   switch (chargingStation.stationInfo?.ocppVersion) {
     case OCPPVersion.VERSION_20:
     case OCPPVersion.VERSION_201:
       if (chargingStation.evses.size === 0) {
-        throw new BaseError('OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration')
+        throw new BaseError(`${chargingStationId}: OCPP 2.0 or superior requires at least one EVSE defined in the charging station template/configuration`)
       }
   }
 }