Improve and fix error handling at sending OCPP commands
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ServiceUtils.ts
index b50ea9f7303adba864042f060038212fd5fb72d4..ead2558aa7cd6d07f94e4e37e81b475f1622bebc 100644 (file)
@@ -10,21 +10,19 @@ export class OCPP16ServiceUtils {
     if (Utils.isUndefined(chargingStation.stationInfo.powerDivider)) {
       const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider is undefined`;
       logger.error(errMsg);
-      throw Error(errMsg);
+      throw new Error(errMsg);
     } else if (chargingStation.stationInfo?.powerDivider <= 0) {
       const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider have zero or below value ${chargingStation.stationInfo.powerDivider}`;
       logger.error(errMsg);
-      throw Error(errMsg);
+      throw new Error(errMsg);
     }
   }
 
   public static buildSampledValue(sampledValueTemplate: SampledValueTemplate, value: number, context?: MeterValueContext, phase?: OCPP16MeterValuePhase): OCPP16SampledValue {
-    const sampledValueValue = value ?? (sampledValueTemplate.value ?? null);
-    const sampledValueContext = context ?? (sampledValueTemplate.context ?? null);
-    const sampledValueLocation = sampledValueTemplate.location
-      ? sampledValueTemplate.location
-      : (OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate.measurand ?? null));
-    const sampledValuePhase = phase ?? (sampledValueTemplate.phase ?? null);
+    const sampledValueValue = value ?? (sampledValueTemplate?.value ?? null);
+    const sampledValueContext = context ?? (sampledValueTemplate?.context ?? null);
+    const sampledValueLocation = sampledValueTemplate?.location ?? OCPP16ServiceUtils.getMeasurandDefaultLocation(sampledValueTemplate?.measurand ?? null);
+    const sampledValuePhase = phase ?? (sampledValueTemplate?.phase ?? null);
     return {
       ...!Utils.isNullOrUndefined(sampledValueTemplate.unit) && { unit: sampledValueTemplate.unit },
       ...!Utils.isNullOrUndefined(sampledValueContext) && { context: sampledValueContext },