From 1c34e5f93827b6f803bd67bdb7a8d74a264b548f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 27 May 2023 00:44:54 +0200 Subject: [PATCH] refactor: factor out default error handling params MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPIncomingRequestService.ts | 2 +- src/performance/storage/Storage.ts | 2 +- src/utils/ErrorUtils.ts | 15 ++++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 1206cede..4d2b29e3 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -62,7 +62,7 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { error: Error, params: HandleErrorParams = { throwError: true, consoleOut: false } ): T | undefined { - ErrorUtils.handleErrorParams(params); + ErrorUtils.setDefaultErrorParams(params); logger.error( `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, error diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index 9df2ebca..39345cdf 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -27,7 +27,7 @@ export abstract class Storage { table?: string, params: HandleErrorParams = { throwError: false, consoleOut: false } ): void { - ErrorUtils.handleErrorParams(params, { throwError: false, consoleOut: false }); + ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false }); const inTableOrCollectionStr = (!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`; logger.error( diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts index 254bd9ec..7569b8eb 100644 --- a/src/utils/ErrorUtils.ts +++ b/src/utils/ErrorUtils.ts @@ -12,6 +12,11 @@ import type { RequestCommand, } from '../types'; +const defaultErrorParams = { + throwError: true, + consoleOut: false, +}; + export class ErrorUtils { private constructor() { // This is intentional @@ -34,9 +39,9 @@ export class ErrorUtils { fileType: FileType, error: NodeJS.ErrnoException, logPrefix: string, - params: HandleErrorParams = { throwError: true, consoleOut: false } + params: HandleErrorParams = defaultErrorParams ): void { - ErrorUtils.handleErrorParams(params); + ErrorUtils.setDefaultErrorParams(params); const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''; let logMsg: string; switch (error.code) { @@ -79,16 +84,16 @@ export class ErrorUtils { error: Error, params: HandleErrorParams = { throwError: false, consoleOut: false } ): void { - ErrorUtils.handleErrorParams(params, { throwError: false, consoleOut: false }); + ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false }); logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); if (params?.throwError === true) { throw error; } } - public static handleErrorParams( + public static setDefaultErrorParams( params: HandleErrorParams, - defaultParams: HandleErrorParams = { throwError: true, consoleOut: false } + defaultParams: HandleErrorParams = defaultErrorParams ): HandleErrorParams { return { ...defaultParams, ...params }; } -- 2.34.1