fix: ensure error at adding charging stations not stop further
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Mar 2024 18:28:36 +0000 (19:28 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 21 Mar 2024 18:28:36 +0000 (19:28 +0100)
processing

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ui-server/ui-services/AbstractUIService.ts

index 6652def2ad2f64e5a43cf30fb1ab0831a9dfdcda..3fa3f8ba756782567755b9a24c4dcf883de99489 100644 (file)
@@ -24,6 +24,12 @@ import type { AbstractUIServer } from '../AbstractUIServer.js'
 
 const moduleName = 'AbstractUIService'
 
+interface AddChargingStationsRequestPayload extends RequestPayload {
+  template: string
+  numberOfStations: number
+  options?: ChargingStationOptions
+}
+
 export abstract class AbstractUIService {
   protected static readonly ProcedureNameToBroadCastChannelProcedureNameMapping = new Map<
   ProcedureName,
@@ -252,11 +258,8 @@ export abstract class AbstractUIService {
     _procedureName?: ProcedureName,
     requestPayload?: RequestPayload
   ): Promise<ResponsePayload> {
-    const { template, numberOfStations, options } = requestPayload as {
-      template: string
-      numberOfStations: number
-      options?: ChargingStationOptions
-    }
+    const { template, numberOfStations, options } =
+      requestPayload as AddChargingStationsRequestPayload
     if (!Bootstrap.getInstance().getState().started) {
       return {
         status: ResponseStatus.FAILURE,
@@ -276,7 +279,9 @@ export abstract class AbstractUIService {
         errorMessage: `Template '${template}' not found`
       } satisfies ResponsePayload
     }
-    const stationInfos: ChargingStationInfo[] = []
+    const succeededStationInfos: ChargingStationInfo[] = []
+    const failedStationInfos: ChargingStationInfo[] = []
+    let err: Error | undefined
     for (let i = 0; i < numberOfStations; i++) {
       let stationInfo: ChargingStationInfo | undefined
       try {
@@ -286,21 +291,23 @@ export abstract class AbstractUIService {
           options
         )
         if (stationInfo != null) {
-          stationInfos.push(stationInfo)
+          succeededStationInfos.push(stationInfo)
         }
       } catch (error) {
-        return {
-          status: ResponseStatus.FAILURE,
-          ...(stationInfo?.hashId != null && { hashIdsFailed: [stationInfo.hashId] }),
-          errorMessage: (error as Error).message,
-          errorStack: (error as Error).stack
-        } satisfies ResponsePayload
+        err = error as Error
+        if (stationInfo != null) {
+          failedStationInfos.push(stationInfo)
+        }
       }
     }
     return {
-      status: ResponseStatus.SUCCESS,
-      hashIdsSucceeded: stationInfos.map(stationInfo => stationInfo.hashId)
-    }
+      status: failedStationInfos.length > 0 ? ResponseStatus.FAILURE : ResponseStatus.SUCCESS,
+      hashIdsSucceeded: succeededStationInfos.map(stationInfo => stationInfo.hashId),
+      ...(failedStationInfos.length > 0 && {
+        hashIdsFailed: failedStationInfos.map(stationInfo => stationInfo.hashId)
+      }),
+      ...(err != null && { errorMessage: err.message, errorStack: err.stack })
+    } satisfies ResponsePayload
   }
 
   private handlePerformanceStatistics (): ResponsePayload {
@@ -321,7 +328,7 @@ export abstract class AbstractUIService {
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           ...Bootstrap.getInstance().getPerformanceStatistics()!
         ] as JsonType[]
-      }
+      } satisfies ResponsePayload
     } catch (error) {
       return {
         status: ResponseStatus.FAILURE,