From 2d4928a7237a906158f2e9652e08f0392eeabdcd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 7 Jun 2024 15:02:00 +0200 Subject: [PATCH] test: improve ErrorUtils coverage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 9 +-- .../ocpp/OCPPIncomingRequestService.ts | 25 +------- src/utils/ErrorUtils.ts | 26 +++++++- src/utils/index.ts | 1 + tests/utils/ErrorUtils.test.ts | 63 ++++++++++++++++++- 5 files changed, 93 insertions(+), 31 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 017de863..64cefe58 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -106,6 +106,7 @@ import { convertToDate, convertToInt, formatDurationMilliSeconds, + handleIncomingRequestError, isAsyncFunction, isNotEmptyArray, isNotEmptyString, @@ -1577,7 +1578,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { }) ftpClient?.close() // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.handleIncomingRequestError( + return handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, error as Error, @@ -1647,7 +1648,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { return OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_UNKNOWN_VENDOR_ID } catch (error) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.handleIncomingRequestError( + return handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.DATA_TRANSFER, error as Error, @@ -1730,7 +1731,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { // 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( + return handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.RESERVE_NOW, error as Error, @@ -1768,7 +1769,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED } catch (error) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.handleIncomingRequestError( + return handleIncomingRequestError( chargingStation, OCPP16IncomingRequestCommand.CANCEL_RESERVATION, error as Error, diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 06165c2f..545438d5 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -7,12 +7,11 @@ import { type ChargingStation, getIdTagsFile } from '../../charging-station/inde 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 @@ -50,28 +49,6 @@ export abstract class OCPPIncomingRequestService extends EventEmitter { return OCPPIncomingRequestService.instance as T } - protected 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 - } - } - protected validateIncomingRequestPayload( chargingStation: ChargingStation, commandName: IncomingRequestCommand, diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts index fb23b133..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 @@ -90,7 +92,7 @@ export const handleSendMessageError = ( ): 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) { @@ -98,6 +100,28 @@ 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 diff --git a/src/utils/index.ts b/src/utils/index.ts index a35887c7..5fedbc57 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,6 +10,7 @@ export { Constants } from './Constants.js' export { ACElectricUtils, DCElectricUtils } from './ElectricUtils.js' export { handleFileException, + handleIncomingRequestError, handleSendMessageError, handleUncaughtException, handleUnhandledRejection, diff --git a/tests/utils/ErrorUtils.test.ts b/tests/utils/ErrorUtils.test.ts index 907d05fa..59c455f9 100644 --- a/tests/utils/ErrorUtils.test.ts +++ b/tests/utils/ErrorUtils.test.ts @@ -2,10 +2,27 @@ import { describe, it } from 'node:test' 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' @@ -19,6 +36,48 @@ await describe('ErrorUtils test suite', async () => { }).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({ -- 2.34.1