refactor: refine OCPP stack log message
[e-mobility-charging-stations-simulator.git] / src / utils / ErrorUtils.ts
index 0e3dbc50058c669c1c5a30a17c8ecbe14e9138c9..537aa308b41a0516e4b688c7f9b38124952d455f 100644 (file)
@@ -1,3 +1,5 @@
+import process from 'node:process';
+
 import chalk from 'chalk';
 
 import { logger } from './Logger';
@@ -34,7 +36,7 @@ export const handleFileException = (
   fileType: FileType,
   error: NodeJS.ErrnoException,
   logPrefix: string,
-  params: HandleErrorParams<EmptyObject> = defaultErrorParams
+  params: HandleErrorParams<EmptyObject> = defaultErrorParams,
 ): void => {
   setDefaultErrorParams(params);
   const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
@@ -56,10 +58,11 @@ export const handleFileException = (
       logMsg = `${fileType} file ${file} error:`;
   }
   if (params?.consoleOut === true) {
+    logMsg = `${logMsg} `;
     if (params?.throwError) {
-      console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
+      console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error);
     } else {
-      console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
+      console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error);
     }
   } else if (params?.consoleOut === false) {
     if (params?.throwError) {
@@ -77,7 +80,7 @@ export const handleSendMessageError = (
   chargingStation: ChargingStation,
   commandName: RequestCommand | IncomingRequestCommand,
   error: Error,
-  params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
+  params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false },
 ): void => {
   setDefaultErrorParams(params, { throwError: false, consoleOut: false });
   logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
@@ -88,7 +91,7 @@ export const handleSendMessageError = (
 
 export const setDefaultErrorParams = <T extends JsonType>(
   params: HandleErrorParams<T>,
-  defaultParams: HandleErrorParams<T> = defaultErrorParams
+  defaultParams: HandleErrorParams<T> = defaultErrorParams,
 ): HandleErrorParams<T> => {
   params = { ...defaultParams, ...params };
   return params;