From: Jérôme Benoit Date: Sun, 30 Jun 2024 11:44:21 +0000 (+0200) Subject: refactor: cleanup default params in error handlers X-Git-Tag: v1.3.7~8 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=64c14c99f9902d4e96f2a2cd404e536e0584a629;p=e-mobility-charging-stations-simulator.git refactor: cleanup default params in error handlers Signed-off-by: Jérôme Benoit --- diff --git a/src/performance/storage/Storage.ts b/src/performance/storage/Storage.ts index ccdbb3f8..39d0a1b3 100644 --- a/src/performance/storage/Storage.ts +++ b/src/performance/storage/Storage.ts @@ -9,7 +9,7 @@ import { type Statistics, StorageType } from '../../types/index.js' -import { logger, setDefaultErrorParams } from '../../utils/index.js' +import { logger } from '../../utils/index.js' export abstract class Storage { protected readonly storageUri: URL @@ -31,7 +31,13 @@ export abstract class Storage { consoleOut: false } ): void { - params = setDefaultErrorParams(params, { throwError: false, consoleOut: false }) + params = { + ...{ + throwError: false, + consoleOut: false + }, + ...params + } const inTableOrCollectionStr = table != null && ` in table or collection '${table}'` logger.error( `${this.logPrefix} ${this.getDBNameFromStorageType(type)} error '${ diff --git a/src/utils/ErrorUtils.ts b/src/utils/ErrorUtils.ts index 0a444c6b..fcbf92b2 100644 --- a/src/utils/ErrorUtils.ts +++ b/src/utils/ErrorUtils.ts @@ -18,11 +18,6 @@ import { isNotEmptyString } from './Utils.js' const moduleName = 'ErrorUtils' -const defaultErrorParams = { - throwError: true, - consoleOut: false -} satisfies HandleErrorParams - export const handleUncaughtException = (): void => { process.on('uncaughtException', (error: Error) => { console.error(chalk.red('Uncaught exception: '), error) @@ -40,9 +35,15 @@ export const handleFileException = ( fileType: FileType, error: NodeJS.ErrnoException, logPrefix: string, - params: HandleErrorParams = defaultErrorParams + params?: HandleErrorParams ): void => { - params = setDefaultErrorParams(params) + params = { + ...{ + throwError: true, + consoleOut: false + }, + ...params + } const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '' let logMsg: string switch (error.code) { @@ -85,12 +86,15 @@ export const handleSendMessageError = ( commandName: RequestCommand | IncomingRequestCommand, messageType: MessageType, error: Error, - params: HandleErrorParams = { - throwError: false, - consoleOut: false - } + params?: HandleErrorParams ): void => { - params = setDefaultErrorParams(params, { throwError: false, consoleOut: false }) + params = { + ...{ + throwError: false, + consoleOut: false + }, + ...params + } logger.error( `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`, error @@ -104,9 +108,15 @@ export const handleIncomingRequestError = ( chargingStation: ChargingStation, commandName: IncomingRequestCommand, error: Error, - params: HandleErrorParams = { throwError: true, consoleOut: false } + params?: HandleErrorParams ): T | undefined => { - params = setDefaultErrorParams(params) + params = { + ...{ + throwError: true, + consoleOut: false + }, + ...params + } logger.error( `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, error @@ -121,10 +131,3 @@ export const handleIncomingRequestError = ( return params.errorResponse } } - -export const setDefaultErrorParams = ( - params: HandleErrorParams, - defaultParams: HandleErrorParams = defaultErrorParams -): HandleErrorParams => { - return { ...defaultParams, ...params } -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 5fedbc57..d6efcdc7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -13,8 +13,7 @@ export { handleIncomingRequestError, handleSendMessageError, handleUncaughtException, - handleUnhandledRejection, - setDefaultErrorParams + handleUnhandledRejection } from './ErrorUtils.js' export { watchJsonFile } from './FileUtils.js' export { logger } from './Logger.js' diff --git a/tests/utils/ErrorUtils.test.ts b/tests/utils/ErrorUtils.test.ts index edf088cf..7bbf4db2 100644 --- a/tests/utils/ErrorUtils.test.ts +++ b/tests/utils/ErrorUtils.test.ts @@ -14,8 +14,7 @@ import { import { handleFileException, handleIncomingRequestError, - handleSendMessageError, - setDefaultErrorParams + handleSendMessageError } from '../../src/utils/ErrorUtils.js' import { logger } from '../../src/utils/Logger.js' @@ -106,24 +105,4 @@ await describe('ErrorUtils test suite', async () => { expect(chargingStation.logPrefix.mock.calls.length).toBe(3) expect(logger.error.mock.calls.length).toBe(3) }) - - await it('Verify setDefaultErrorParams()', () => { - expect(setDefaultErrorParams({})).toStrictEqual({ throwError: true, consoleOut: false }) - expect(setDefaultErrorParams({ throwError: false })).toStrictEqual({ - throwError: false, - consoleOut: false - }) - expect(setDefaultErrorParams({ throwError: false, consoleOut: true })).toStrictEqual({ - throwError: false, - consoleOut: true - }) - expect(setDefaultErrorParams({ throwError: true, consoleOut: true })).toStrictEqual({ - throwError: true, - consoleOut: true - }) - expect(setDefaultErrorParams({}, { throwError: false, consoleOut: false })).toStrictEqual({ - throwError: false, - consoleOut: false - }) - }) })