]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
fix(ocpp): graceful OCPPError fallback instead of throw in buildMessageToSend
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 16 Mar 2026 15:54:52 +0000 (16:54 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 16 Mar 2026 15:54:52 +0000 (16:54 +0100)
Replace defensive throw with OCPPError construction when messagePayload
is not an OCPPError instance. Uses InternalError code per OCPP-J §4.2.3.
Harmonizes with the ternary instanceof pattern used at L452/503 in the
same file for errorDetails extraction.

src/charging-station/ocpp/OCPPRequestService.ts

index b9922d7260588733544764eac453ecb6196e3532..01364af193851316e60c4dca871234e5cb23eaa3 100644 (file)
@@ -26,6 +26,7 @@ import {
   clone,
   ensureError,
   formatDurationMilliSeconds,
+  getErrorMessage,
   handleSendMessageError,
   logger,
 } from '../../utils/index.js'
@@ -274,25 +275,23 @@ export abstract class OCPPRequestService {
     // Type of message
     switch (messageType) {
       // Error Message
-      case MessageType.CALL_ERROR_MESSAGE:
-        // Build Error Message
-        if (!(messagePayload instanceof OCPPError)) {
-          throw new OCPPError(
-            ErrorType.INTERNAL_ERROR,
-            `Expected OCPPError instance for CALL_ERROR_MESSAGE, got ${typeof messagePayload}`,
-            commandName
-          )
-        }
+      case MessageType.CALL_ERROR_MESSAGE: {
+        // Build Error Message per OCPP-J §4.2.3: [4, messageId, errorCode, errorDescription, errorDetails]
+        const ocppError =
+          messagePayload instanceof OCPPError
+            ? messagePayload
+            : new OCPPError(ErrorType.INTERNAL_ERROR, getErrorMessage(messagePayload), commandName)
         messageToSend = JSON.stringify([
           messageType,
           messageId,
-          messagePayload.code,
-          messagePayload.message,
-          messagePayload.details ?? {
-            command: messagePayload.command,
+          ocppError.code,
+          ocppError.message,
+          ocppError.details ?? {
+            command: ocppError.command,
           },
         ] satisfies ErrorResponse)
         break
+      }
       // Request
       case MessageType.CALL_MESSAGE:
         // Build request