fix: ensure error at WS message sending is handled
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 15:45:41 +0000 (16:45 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 15:45:41 +0000 (16:45 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/OCPPRequestService.ts

index 60d8f8b4a28576dcd0bfbc40dadccdc9431851e8..e12e6d17bca9da98ff7733c9521afbddb895c99e 100644 (file)
@@ -1064,14 +1064,17 @@ export class ChargingStation extends EventEmitter {
           [, , commandName] = JSON.parse(message) as OutgoingRequest;
           beginId = PerformanceStatistics.beginMeasure(commandName);
         }
-        this.wsConnection?.send(message);
-        isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!);
-        logger.debug(
-          `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
-            messageType,
-          )} payload sent: ${message}`,
-        );
-        this.messageBuffer.delete(message);
+        this.wsConnection?.send(message, (error?: Error) => {
+          isRequest && PerformanceStatistics.endMeasure(commandName!, beginId!);
+          if (isNullOrUndefined(error)) {
+            logger.debug(
+              `${this.logPrefix()} >> Buffered ${OCPPServiceUtils.getMessageTypeString(
+                messageType,
+              )} payload sent: ${message}`,
+            );
+            this.messageBuffer.delete(message);
+          }
+        });
       }
     }
   }
index 01fd3d3499782690c5095427100b43ec2ae328a7..f1ea87112dcfbfaed5fb261418e0cd5d396df5e3 100644 (file)
@@ -23,7 +23,13 @@ import {
   type ResponseCallback,
   type ResponseType,
 } from '../../types';
-import { Constants, cloneObject, handleSendMessageError, logger } from '../../utils';
+import {
+  Constants,
+  cloneObject,
+  handleSendMessageError,
+  isNullOrUndefined,
+  logger,
+} from '../../utils';
 
 const moduleName = 'OCPPRequestService';
 
@@ -379,6 +385,23 @@ export abstract class OCPPRequestService {
           return errorCallback(ocppError, false);
         };
 
+        const bufferAndRejectWithOcppError = (ocppError: OCPPError): void => {
+          // Buffer
+          chargingStation.bufferMessage(messageToSend);
+          if (messageType === MessageType.CALL_MESSAGE) {
+            this.cacheRequestPromise(
+              chargingStation,
+              messageId,
+              messagePayload as JsonType,
+              commandName,
+              responseCallback,
+              errorCallback,
+            );
+          }
+          // Reject and keep request in the cache
+          return reject(ocppError);
+        };
+
         if (chargingStation.stationInfo?.enableStatistics === true) {
           chargingStation.performanceStatistics?.addRequestStatistic(commandName, messageType);
         }
@@ -403,8 +426,29 @@ export abstract class OCPPRequestService {
             );
           }, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT);
           chargingStation.wsConnection?.send(messageToSend, (error?: Error) => {
+            PerformanceStatistics.endMeasure(commandName, beginId);
             clearTimeout(sendTimeout);
-            if (error) {
+            if (isNullOrUndefined(error)) {
+              logger.debug(
+                `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
+                  messageType,
+                )} payload: ${messageToSend}`,
+              );
+              if (messageType === MessageType.CALL_MESSAGE) {
+                this.cacheRequestPromise(
+                  chargingStation,
+                  messageId,
+                  messagePayload as JsonType,
+                  commandName,
+                  responseCallback,
+                  errorCallback,
+                );
+              }
+              // Resolve response
+              if (messageType !== MessageType.CALL_MESSAGE) {
+                return resolve(messagePayload);
+              }
+            } else if (error) {
               const ocppError = new OCPPError(
                 ErrorType.GENERIC_ERROR,
                 `WebSocket errored for ${
@@ -414,30 +458,11 @@ export abstract class OCPPRequestService {
                 { name: error.name, message: error.message, stack: error.stack },
               );
               if (params?.skipBufferingOnError === false) {
-                // Buffer
-                chargingStation.bufferMessage(messageToSend);
-                // Reject and keep request in the cache
-                return reject(ocppError);
+                return bufferAndRejectWithOcppError(ocppError);
               }
               return rejectWithOcppError(ocppError);
             }
           });
-          if (messageType === MessageType.CALL_MESSAGE) {
-            this.cacheRequestPromise(
-              chargingStation,
-              messageId,
-              messagePayload as JsonType,
-              commandName,
-              responseCallback,
-              errorCallback,
-            );
-          }
-          logger.debug(
-            `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
-              messageType,
-            )} payload: ${messageToSend}`,
-          );
-          PerformanceStatistics.endMeasure(commandName, beginId);
         } else {
           const ocppError = new OCPPError(
             ErrorType.GENERIC_ERROR,
@@ -448,27 +473,10 @@ export abstract class OCPPRequestService {
             (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
           );
           if (params?.skipBufferingOnError === false) {
-            // Buffer
-            chargingStation.bufferMessage(messageToSend);
-            if (messageType === MessageType.CALL_MESSAGE) {
-              this.cacheRequestPromise(
-                chargingStation,
-                messageId,
-                messagePayload as JsonType,
-                commandName,
-                responseCallback,
-                errorCallback,
-              );
-            }
-            // Reject and keep request in the cache
-            return reject(ocppError);
+            return bufferAndRejectWithOcppError(ocppError);
           }
           return rejectWithOcppError(ocppError);
         }
-        // Resolve response
-        if (messageType !== MessageType.CALL_MESSAGE) {
-          return resolve(messagePayload);
-        }
       });
     }
     throw new OCPPError(