X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils%2FErrorUtils.ts;h=0a444c6b1f5d945e5577dd7fbebc6ec235df16e6;hb=b8e3363a179fcf79d8bb66f47724b377d4d38a75;hp=f74c48e8859923ce0eec21976e3e60c089072239;hpb=436569b1470c8e6187c63d974304bd1b99f35ba3;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts index f74c48e8..0a444c6b 100644 --- a/src/utils/ErrorUtils.ts +++ b/src/utils/ErrorUtils.ts @@ -16,6 +16,8 @@ import type { import { logger } from './Logger.js' import { isNotEmptyString } from './Utils.js' +const moduleName = 'ErrorUtils' + const defaultErrorParams = { throwError: true, consoleOut: false @@ -40,7 +42,7 @@ export const handleFileException = ( logPrefix: string, params: HandleErrorParams = defaultErrorParams ): void => { - setDefaultErrorParams(params) + params = setDefaultErrorParams(params) const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '' let logMsg: string switch (error.code) { @@ -88,9 +90,9 @@ export const handleSendMessageError = ( consoleOut: false } ): void => { - setDefaultErrorParams(params, { throwError: false, consoleOut: false }) + params = setDefaultErrorParams(params, { throwError: false, consoleOut: false }) logger.error( - `${chargingStation.logPrefix()} Send ${getMessageTypeString(messageType)} command '${commandName}' error:`, + `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`, error ) if (params.throwError === true) { @@ -98,10 +100,31 @@ export const handleSendMessageError = ( } } +export const handleIncomingRequestError = ( + chargingStation: ChargingStation, + commandName: IncomingRequestCommand, + error: Error, + params: HandleErrorParams = { throwError: true, consoleOut: false } +): T | undefined => { + params = setDefaultErrorParams(params) + logger.error( + `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, + error + ) + if (params.throwError === false && params.errorResponse != null) { + return params.errorResponse + } + if (params.throwError === true && params.errorResponse == null) { + throw error + } + if (params.throwError === true && params.errorResponse != null) { + return params.errorResponse + } +} + export const setDefaultErrorParams = ( params: HandleErrorParams, defaultParams: HandleErrorParams = defaultErrorParams ): HandleErrorParams => { - params = { ...defaultParams, ...params } - return params + return { ...defaultParams, ...params } }