From: Jérôme Benoit Date: Thu, 21 Mar 2024 18:28:36 +0000 (+0100) Subject: fix: ensure error at adding charging stations not stop further X-Git-Tag: v1.3.1~18 X-Git-Url: https://git.piment-noir.org/?p=e-mobility-charging-stations-simulator.git;a=commitdiff_plain;h=1091427efe95a8f1733282589dc1794a5be7e785 fix: ensure error at adding charging stations not stop further processing Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 6652def2..3fa3f8ba 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -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 { - 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,