From 717c1e56bde34461c2a53b73cc1ca04aed39392f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 4 Mar 2022 20:37:33 +0100 Subject: [PATCH] Fix incoming message error handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/Bootstrap.ts | 17 +++++++++++------ .../ocpp/1.6/OCPP16IncomingRequestService.ts | 4 ++-- .../ocpp/OCPPIncomingRequestService.ts | 11 +++++++---- src/performance/storage/Storage.ts | 3 ++- src/types/Error.ts | 3 ++- src/utils/Configuration.ts | 4 +++- src/utils/FileUtils.ts | 3 ++- 7 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 3aab9416..fa8f73eb 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -3,6 +3,7 @@ import { ChargingStationWorkerData, ChargingStationWorkerMessage, ChargingStationWorkerMessageEvents } from '../types/ChargingStationWorker'; import Configuration from '../utils/Configuration'; +import { StationTemplateUrl } from '../types/ConfigurationData'; import Statistics from '../types/Statistics'; import { Storage } from '../performance/storage/Storage'; import { StorageFactory } from '../performance/storage/StorageFactory'; @@ -62,12 +63,7 @@ export default class Bootstrap { try { const nbStations = stationTemplateUrl.numberOfStations ?? 0; for (let index = 1; index <= nbStations; index++) { - const workerData: ChargingStationWorkerData = { - index, - templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationTemplateUrl.file)) - }; - await this.workerImplementation.addElement(workerData); - this.numberOfChargingStations++; + await this.startChargingStation(index, stationTemplateUrl); } } catch (error) { console.error(chalk.red('Charging station start with template file ' + stationTemplateUrl.file + ' error '), error); @@ -130,6 +126,15 @@ export default class Bootstrap { }); } + private async startChargingStation(index: number, stationTemplateUrl: StationTemplateUrl): Promise { + const workerData: ChargingStationWorkerData = { + index, + templateFile: path.join(path.resolve(__dirname, '../'), 'assets', 'station-templates', path.basename(stationTemplateUrl.file)) + }; + await this.workerImplementation.addElement(workerData); + this.numberOfChargingStations++; + } + private logPrefix(): string { return Utils.logPrefix(' Bootstrap |'); } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 5c7f7bf6..a9b79c13 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -423,7 +423,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer if (ftpClient) { ftpClient.close(); } - return this.handleIncomingRequestError(OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, error as Error, Constants.OCPP_RESPONSE_EMPTY); + return this.handleIncomingRequestError(OCPP16IncomingRequestCommand.GET_DIAGNOSTICS, error as Error, { errorResponse: Constants.OCPP_RESPONSE_EMPTY }); } } else { logger.error(`${this.chargingStation.logPrefix()} Unsupported protocol ${uri.protocol} to transfer the diagnostic logs archive`); @@ -454,7 +454,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED; } } catch (error) { - return this.handleIncomingRequestError(OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, error as Error, Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED); + return this.handleIncomingRequestError(OCPP16IncomingRequestCommand.TRIGGER_MESSAGE, error as Error, { errorResponse: Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }); } } } diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index a56437f9..5fadfb95 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -19,14 +19,17 @@ export default abstract class OCPPIncomingRequestService { return OCPPIncomingRequestService.instances.get(chargingStation.id) as T; } - protected handleIncomingRequestError(commandName: IncomingRequestCommand, error: Error, errorOcppResponse?: T, params: HandleErrorParams = { throwError: true }): T { + protected handleIncomingRequestError(commandName: IncomingRequestCommand, error: Error, params: HandleErrorParams = { throwError: true }): T { logger.error(this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', commandName, error); - if (errorOcppResponse) { - return errorOcppResponse; + if (!params?.throwError && params?.errorResponse) { + return params?.errorResponse; } - if (params?.throwError) { + if (params?.throwError && !params?.errorResponse) { throw error; } + if (params?.throwError && params?.errorResponse) { + return params?.errorResponse; + } } public abstract handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: JsonType): Promise; diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index 2f5fecce..c4a0ce9c 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -2,6 +2,7 @@ import { DBName, StorageType } from '../../types/Storage'; +import { EmptyObject } from '../../types/EmptyObject'; import { HandleErrorParams } from '../../types/Error'; import Statistics from '../../types/Statistics'; import { URL } from 'url'; @@ -18,7 +19,7 @@ export abstract class Storage { this.logPrefix = logPrefix; } - protected handleDBError(type: StorageType, error: Error, table?: string, params: HandleErrorParams = { throwError: false }): void { + protected handleDBError(type: StorageType, error: Error, table?: string, params: HandleErrorParams = { throwError: false }): void { logger.error(`${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${error.message}'${(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`}: %j`, error); if (params?.throwError) { throw error; diff --git a/src/types/Error.ts b/src/types/Error.ts index c599438c..b27f2457 100644 --- a/src/types/Error.ts +++ b/src/types/Error.ts @@ -1,4 +1,5 @@ -export interface HandleErrorParams { +export interface HandleErrorParams { throwError?: boolean; consoleOut?: boolean; + errorResponse?: T; } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 86c93358..57d17c8f 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -1,6 +1,7 @@ import ConfigurationData, { StationTemplateUrl, StorageConfiguration, SupervisionUrlDistribution, UIWebSocketServerConfiguration } from '../types/ConfigurationData'; import Constants from './Constants'; +import { EmptyObject } from '../types/EmptyObject'; import { HandleErrorParams } from '../types/Error'; import { ServerOptions } from 'ws'; import { StorageType } from '../types/Storage'; @@ -233,7 +234,8 @@ export default class Configuration { return typeof obj === 'undefined'; } - private static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, params: HandleErrorParams = { throwError: true }): void { + private static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, + params: HandleErrorParams = { throwError: true }): void { const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; if (error.code === 'ENOENT') { console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' not found: '), error); diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index a0d495a6..014ebbde 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,10 +1,11 @@ +import { EmptyObject } from '../types/EmptyObject'; import { HandleErrorParams } from '../types/Error'; import chalk from 'chalk'; import logger from './Logger'; export default class FileUtils { static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, - params: HandleErrorParams = { throwError: true, consoleOut: false }): void { + params: HandleErrorParams = { throwError: true, consoleOut: false }): void { const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; if (error.code === 'ENOENT') { if (params?.consoleOut) { -- 2.34.1