refactor: refine OCPP message sending error handling
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 4 Jun 2024 23:17:08 +0000 (01:17 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 4 Jun 2024 23:17:08 +0000 (01:17 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/ocpp/OCPPRequestService.ts
src/utils/ErrorUtils.ts

index a3eca0700ac40126ca601d3471ba11a4c9c26a8d..d396f354820d785613b2b677fa44d534f4b34d31 100644 (file)
@@ -28,7 +28,7 @@ import {
 } from '../../utils/index.js'
 import { OCPPConstants } from './OCPPConstants.js'
 import type { OCPPResponseService } from './OCPPResponseService.js'
-import { OCPPServiceUtils } from './OCPPServiceUtils.js'
+import { getMessageTypeString, OCPPServiceUtils } from './OCPPServiceUtils.js'
 type Ajv = _Ajv.default
 // eslint-disable-next-line @typescript-eslint/no-redeclare
 const Ajv = _Ajv.default
@@ -94,9 +94,15 @@ export abstract class OCPPRequestService {
         commandName
       )
     } catch (error) {
-      handleSendMessageError(chargingStation, commandName, error as Error, {
-        throwError: true
-      })
+      handleSendMessageError(
+        chargingStation,
+        commandName,
+        MessageType.CALL_RESULT_MESSAGE,
+        error as Error,
+        {
+          throwError: true
+        }
+      )
       return null
     }
   }
@@ -117,7 +123,12 @@ export abstract class OCPPRequestService {
         commandName
       )
     } catch (error) {
-      handleSendMessageError(chargingStation, commandName, error as Error)
+      handleSendMessageError(
+        chargingStation,
+        commandName,
+        MessageType.CALL_ERROR_MESSAGE,
+        error as Error
+      )
       return null
     }
   }
@@ -143,9 +154,15 @@ export abstract class OCPPRequestService {
         params
       )
     } catch (error) {
-      handleSendMessageError(chargingStation, commandName, error as Error, {
-        throwError: params.throwError
-      })
+      handleSendMessageError(
+        chargingStation,
+        commandName,
+        MessageType.CALL_MESSAGE,
+        error as Error,
+        {
+          throwError: params.throwError
+        }
+      )
       return null
     }
   }
@@ -291,7 +308,7 @@ export abstract class OCPPRequestService {
             )
           }
           logger.error(
-            `${chargingStation.logPrefix()} Error occurred at ${OCPPServiceUtils.getMessageTypeString(
+            `${chargingStation.logPrefix()} Error occurred at ${getMessageTypeString(
               messageType
             )} command ${commandName} with PDU %j:`,
             messagePayload,
@@ -358,7 +375,7 @@ export abstract class OCPPRequestService {
             clearTimeout(sendTimeout)
             if (error == null) {
               logger.debug(
-                `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
+                `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${getMessageTypeString(
                   messageType
                 )} payload: ${messageToSend}`
               )
index 36a5f04ca944df16e9b37bb186e1deb54dd01c12..f74c48e8859923ce0eec21976e3e60c089072239 100644 (file)
@@ -3,12 +3,14 @@ import process from 'node:process'
 import chalk from 'chalk'
 
 import type { ChargingStation } from '../charging-station/index.js'
+import { getMessageTypeString } from '../charging-station/ocpp/OCPPServiceUtils.js'
 import type {
   EmptyObject,
   FileType,
   HandleErrorParams,
   IncomingRequestCommand,
   JsonType,
+  MessageType,
   RequestCommand
 } from '../types/index.js'
 import { logger } from './Logger.js'
@@ -79,6 +81,7 @@ export const handleFileException = (
 export const handleSendMessageError = (
   chargingStation: ChargingStation,
   commandName: RequestCommand | IncomingRequestCommand,
+  messageType: MessageType,
   error: Error,
   params: HandleErrorParams<EmptyObject> = {
     throwError: false,
@@ -86,7 +89,10 @@ export const handleSendMessageError = (
   }
 ): void => {
   setDefaultErrorParams(params, { throwError: false, consoleOut: false })
-  logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error)
+  logger.error(
+    `${chargingStation.logPrefix()} Send ${getMessageTypeString(messageType)} command '${commandName}' error:`,
+    error
+  )
   if (params.throwError === true) {
     throw error
   }