convertToDate,
convertToInt,
formatDurationMilliSeconds,
+ handleIncomingRequestError,
isAsyncFunction,
isNotEmptyArray,
isNotEmptyString,
})
ftpClient?.close()
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.handleIncomingRequestError<GetDiagnosticsResponse>(
+ return handleIncomingRequestError<GetDiagnosticsResponse>(
chargingStation,
OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
error as Error,
return OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.handleIncomingRequestError<OCPP16DataTransferResponse>(
+ return handleIncomingRequestError<OCPP16DataTransferResponse>(
chargingStation,
OCPP16IncomingRequestCommand.DATA_TRANSFER,
error as Error,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
chargingStation.getConnectorStatus(connectorId)!.status = OCPP16ChargePointStatus.Available
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.handleIncomingRequestError<OCPP16ReserveNowResponse>(
+ return handleIncomingRequestError<OCPP16ReserveNowResponse>(
chargingStation,
OCPP16IncomingRequestCommand.RESERVE_NOW,
error as Error,
return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.handleIncomingRequestError<GenericResponse>(
+ return handleIncomingRequestError<GenericResponse>(
chargingStation,
OCPP16IncomingRequestCommand.CANCEL_RESERVATION,
error as Error,
import { OCPPError } from '../../exception/index.js'
import type {
ClearCacheResponse,
- HandleErrorParams,
IncomingRequestCommand,
JsonType,
OCPPVersion
} from '../../types/index.js'
-import { logger, setDefaultErrorParams } from '../../utils/index.js'
+import { logger } from '../../utils/index.js'
import { OCPPConstants } from './OCPPConstants.js'
import { ajvErrorsToErrorType } from './OCPPServiceUtils.js'
type Ajv = _Ajv.default
return OCPPIncomingRequestService.instance as T
}
- protected handleIncomingRequestError<T extends JsonType>(
- chargingStation: ChargingStation,
- commandName: IncomingRequestCommand,
- error: Error,
- params: HandleErrorParams<T> = { 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
- }
- }
-
protected validateIncomingRequestPayload<T extends JsonType>(
chargingStation: ChargingStation,
commandName: IncomingRequestCommand,
import { logger } from './Logger.js'
import { isNotEmptyString } from './Utils.js'
+const moduleName = 'ErrorUtils'
+
const defaultErrorParams = {
throwError: true,
consoleOut: false
): void => {
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) {
}
}
+export const handleIncomingRequestError = <T extends JsonType>(
+ chargingStation: ChargingStation,
+ commandName: IncomingRequestCommand,
+ error: Error,
+ params: HandleErrorParams<T> = { 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 = <T extends JsonType>(
params: HandleErrorParams<T>,
defaultParams: HandleErrorParams<T> = defaultErrorParams
export { ACElectricUtils, DCElectricUtils } from './ElectricUtils.js'
export {
handleFileException,
+ handleIncomingRequestError,
handleSendMessageError,
handleUncaughtException,
handleUnhandledRejection,
import { expect } from 'expect'
-import { FileType } from '../../src/types/index.js'
-import { handleFileException, setDefaultErrorParams } from '../../src/utils/ErrorUtils.js'
+import type { ChargingStation } from '../../src/charging-station/index.js'
+import {
+ FileType,
+ GenericStatus,
+ IncomingRequestCommand,
+ MessageType,
+ RequestCommand
+} from '../../src/types/index.js'
+import {
+ handleFileException,
+ handleIncomingRequestError,
+ handleSendMessageError,
+ setDefaultErrorParams
+} from '../../src/utils/ErrorUtils.js'
await describe('ErrorUtils test suite', async () => {
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
+ const chargingStation = {
+ logPrefix: () => 'CS-TEST |'
+ } as ChargingStation
+
await it('Verify handleFileException()', () => {
const error = new Error()
error.code = 'ENOENT'
}).not.toThrow()
})
+ await it('Verify handleSendMessageError()', () => {
+ const error = new Error()
+ expect(() => {
+ handleSendMessageError(
+ chargingStation,
+ RequestCommand.BOOT_NOTIFICATION,
+ MessageType.CALL_MESSAGE,
+ error
+ )
+ }).not.toThrow()
+ expect(() => {
+ handleSendMessageError(
+ chargingStation,
+ RequestCommand.BOOT_NOTIFICATION,
+ MessageType.CALL_MESSAGE,
+ error,
+ { throwError: true }
+ )
+ }).toThrow(error)
+ })
+
+ await it('Verify handleIncomingRequestError()', () => {
+ const error = new Error()
+ expect(() => {
+ handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error)
+ }).toThrow(error)
+ expect(() => {
+ handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, {
+ throwError: false
+ })
+ }).not.toThrow()
+ const errorResponse = {
+ status: GenericStatus.Rejected
+ }
+ expect(
+ handleIncomingRequestError(chargingStation, IncomingRequestCommand.CLEAR_CACHE, error, {
+ throwError: false,
+ errorResponse
+ })
+ ).toStrictEqual(errorResponse)
+ })
+
await it('Verify setDefaultErrorParams()', () => {
expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false })
expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({