From: Jérôme Benoit Date: Mon, 16 Mar 2026 15:54:52 +0000 (+0100) Subject: fix(ocpp): graceful OCPPError fallback instead of throw in buildMessageToSend X-Git-Tag: ocpp-server@v3.1.1~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=927f8912a498d2f1b4259d6eb95029b0e5bcb628;p=e-mobility-charging-stations-simulator.git fix(ocpp): graceful OCPPError fallback instead of throw in buildMessageToSend 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. --- diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index b9922d72..01364af1 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -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