From e0a50bcd7ae5a1b4ce22e39935f96a8f7e1222b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 24 Feb 2022 14:34:01 +0100 Subject: [PATCH] Do not throw an error at OCPP message sending to avoid crashing the worker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPIncomingRequestService.ts | 7 +++++-- src/charging-station/ocpp/OCPPRequestService.ts | 9 ++++++--- src/performance/storage/Storage.ts | 6 +++++- src/types/Error.ts | 4 ++++ src/utils/Configuration.ts | 7 +++++-- src/utils/FileUtils.ts | 16 ++++++++++------ 6 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 src/types/Error.ts diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 3fc960a9..a56437f9 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,4 +1,5 @@ import type ChargingStation from '../ChargingStation'; +import { HandleErrorParams } from '../../types/Error'; import { IncomingRequestCommand } from '../../types/ocpp/Requests'; import { JsonType } from '../../types/JsonType'; import logger from '../../utils/Logger'; @@ -18,12 +19,14 @@ export default abstract class OCPPIncomingRequestService { return OCPPIncomingRequestService.instances.get(chargingStation.id) as T; } - protected handleIncomingRequestError(commandName: IncomingRequestCommand, error: Error, errorOcppResponse?: T): T { + protected handleIncomingRequestError(commandName: IncomingRequestCommand, error: Error, errorOcppResponse?: T, params: HandleErrorParams = { throwError: true }): T { logger.error(this.chargingStation.logPrefix() + ' Incoming request command %s error: %j', commandName, error); if (errorOcppResponse) { return errorOcppResponse; } - throw error; + if (params?.throwError) { + throw error; + } } public abstract handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: JsonType): Promise; diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 000d8c0a..db3ce5e0 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -7,6 +7,7 @@ import { ChargePointStatus } from '../../types/ocpp/ChargePointStatus'; import type ChargingStation from '../ChargingStation'; import Constants from '../../utils/Constants'; import { ErrorType } from '../../types/ocpp/ErrorType'; +import { HandleErrorParams } from '../../types/Error'; import { JsonType } from '../../types/JsonType'; import { MessageType } from '../../types/ocpp/MessageType'; import { MeterValue } from '../../types/ocpp/MeterValues'; @@ -58,7 +59,7 @@ export default abstract class OCPPRequestService { try { return await this.internalSendMessage(messageId, messagePayload, MessageType.CALL_MESSAGE, commandName, params); } catch (error) { - this.handleRequestError(commandName, error as Error); + this.handleRequestError(commandName, error as Error, { throwError: false }); } } @@ -173,9 +174,11 @@ export default abstract class OCPPRequestService { return messageToSend; } - private handleRequestError(commandName: RequestCommand | IncomingRequestCommand, error: Error): void { + private handleRequestError(commandName: RequestCommand | IncomingRequestCommand, error: Error, params: HandleErrorParams = { throwError: true }): void { logger.error(this.chargingStation.logPrefix() + ' Request command %s error: %j', commandName, error); - throw error; + if (params?.throwError) { + throw error; + } } public abstract sendHeartbeat(params?: SendParams): Promise; diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index db33a694..2f5fecce 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -2,6 +2,7 @@ import { DBName, StorageType } from '../../types/Storage'; +import { HandleErrorParams } from '../../types/Error'; import Statistics from '../../types/Statistics'; import { URL } from 'url'; import Utils from '../../utils/Utils'; @@ -17,8 +18,11 @@ export abstract class Storage { this.logPrefix = logPrefix; } - protected handleDBError(type: StorageType, error: Error, table?: string): 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; + } } protected getDBNameFromStorageType(type: StorageType): DBName { diff --git a/src/types/Error.ts b/src/types/Error.ts new file mode 100644 index 00000000..c599438c --- /dev/null +++ b/src/types/Error.ts @@ -0,0 +1,4 @@ +export interface HandleErrorParams { + throwError?: boolean; + consoleOut?: boolean; +} diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 519f0465..d88526a2 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 { HandleErrorParams } from '../types/Error'; import { ServerOptions } from 'ws'; import { StorageType } from '../types/Storage'; import type { WorkerChoiceStrategy } from 'poolifier'; @@ -228,7 +229,7 @@ export default class Configuration { return typeof obj === 'undefined'; } - private static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException): 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); @@ -239,6 +240,8 @@ export default class Configuration { } else { console.error(chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' error: '), error); } - throw error; + if (params?.throwError) { + throw error; + } } } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index e68500e3..a0d495a6 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -1,34 +1,38 @@ +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, consoleOut = false): void { + static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, + params: HandleErrorParams = { throwError: true, consoleOut: false }): void { const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; if (error.code === 'ENOENT') { - if (consoleOut) { + if (params?.consoleOut) { console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '), error); } else { logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); } } else if (error.code === 'EEXIST') { - if (consoleOut) { + if (params?.consoleOut) { console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '), error); } else { logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error); } } else if (error.code === 'EACCES') { - if (consoleOut) { + if (params?.consoleOut) { console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '), error); } else { logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error); } } else { - if (consoleOut) { + if (params?.consoleOut) { console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '), error); } else { logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error); } - throw error; + if (params?.throwError) { + throw error; + } } } } -- 2.34.1