]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: consistent error handling helpers API
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 15:45:35 +0000 (17:45 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 13 Aug 2025 15:45:35 +0000 (17:45 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/utils/ErrorUtils.ts

index 7c0afb283aabf5f24336948b5bd3088220f024bb..2d64527debc0e381ee828b3cceff3419608085c8 100644 (file)
@@ -694,6 +694,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         error as Error,
         {
           errorResponse: OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED,
+          throwError: false,
         }
       )!
     }
@@ -913,7 +914,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         chargingStation,
         OCPP16IncomingRequestCommand.DATA_TRANSFER,
         error as Error,
-        { errorResponse: OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED }
+        { errorResponse: OCPP16Constants.OCPP_DATA_TRANSFER_RESPONSE_REJECTED, throwError: false }
       )!
     }
   }
@@ -1179,7 +1180,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           chargingStation,
           OCPP16IncomingRequestCommand.GET_DIAGNOSTICS,
           error as Error,
-          { errorResponse: OCPP16Constants.OCPP_RESPONSE_EMPTY }
+          { errorResponse: OCPP16Constants.OCPP_RESPONSE_EMPTY, throwError: false }
         )!
       }
     } else {
@@ -1373,7 +1374,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         chargingStation,
         OCPP16IncomingRequestCommand.RESERVE_NOW,
         error as Error,
-        { errorResponse: OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED }
+        { errorResponse: OCPP16Constants.OCPP_RESERVATION_RESPONSE_FAULTED, throwError: false }
       )!
     }
   }
index 808c2b5390cb1bcd81d358d1fb6f78bf63ab5186..75998b96506fc9ad335a2537c451eba972bb1c00 100644 (file)
@@ -21,12 +21,16 @@ const moduleName = 'ErrorUtils'
 export const handleUncaughtException = (): void => {
   process.on('uncaughtException', (error: Error) => {
     console.error(chalk.red('Uncaught exception: '), error)
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+    logger?.error?.('Uncaught exception:', error)
   })
 }
 
 export const handleUnhandledRejection = (): void => {
-  process.on('unhandledRejection', (reason: unknown) => {
-    console.error(chalk.red('Unhandled rejection: '), reason)
+  process.on('unhandledRejection', (reason: unknown, promise: Promise<unknown>) => {
+    console.error(chalk.red('Unhandled rejection: '), { promise, reason })
+    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
+    logger?.error?.('Unhandled rejection:', { promise, reason })
   })
 }
 
@@ -53,12 +57,24 @@ export const handleFileException = (
     case 'EEXIST':
       logMsg = `${fileType} file ${file} already exists:`
       break
+    case 'EISDIR':
+      logMsg = `${fileType} file ${file} is a directory:`
+      break
     case 'ENOENT':
       logMsg = `${fileType} file ${file} not found:`
       break
+    case 'ENOSPC':
+      logMsg = `${fileType} file ${file} no space left on device:`
+      break
+    case 'ENOTDIR':
+      logMsg = `${fileType} file ${file} parent is not a directory:`
+      break
     case 'EPERM':
       logMsg = `${fileType} file ${file} permission denied:`
       break
+    case 'EROFS':
+      logMsg = `${fileType} file ${file} read-only file system:`
+      break
     default:
       logMsg = `${fileType} file ${file} error:`
   }
@@ -95,10 +111,12 @@ export const handleSendMessageError = (
     },
     ...params,
   }
-  logger.error(
-    `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`,
-    error
-  )
+  const logMsg = `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`
+  if (params.consoleOut === true) {
+    console.error(logMsg, error)
+  } else {
+    logger.error(logMsg, error)
+  }
   if (params.throwError === true) {
     throw error
   }
@@ -117,17 +135,14 @@ export const handleIncomingRequestError = <T extends JsonType>(
     },
     ...params,
   }
-  logger.error(
-    `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
-    error
-  )
-  if (params.throwError === false && params.errorResponse != null) {
-    return params.errorResponse
-  }
-  if (params.throwError === true && params.errorResponse == null) {
-    throw error
+  const logMsg = `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`
+  if (params.consoleOut === true) {
+    console.error(logMsg, error)
+  } else {
+    logger.error(logMsg, error)
   }
-  if (params.throwError === true && params.errorResponse != null) {
+  if (params.throwError === false) {
     return params.errorResponse
   }
+  throw error
 }