refactor: factor out WS error handling code
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 16:11:25 +0000 (17:11 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 19 Nov 2023 16:11:25 +0000 (17:11 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/OCPPRequestService.ts

index 0d59131fd157804c3de02cd4037fde289d82beed..25305ca1ebaeb0885cead2bdad12a2fae2551e0c 100644 (file)
@@ -385,21 +385,24 @@ 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,
-            );
+        const handleSendError = (ocppError: OCPPError): void => {
+          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);
           }
-          // Reject and keep request in the cache
-          return reject(ocppError);
+          return rejectWithOcppError(ocppError);
         };
 
         if (chargingStation.stationInfo?.enableStatistics === true) {
@@ -448,33 +451,29 @@ export abstract class OCPPRequestService {
                 return resolve(messagePayload);
               }
             } else if (error) {
-              const ocppError = new OCPPError(
-                ErrorType.GENERIC_ERROR,
-                `WebSocket errored for ${
-                  params?.skipBufferingOnError === false ? '' : 'non '
-                }buffered message id '${messageId}' with content '${messageToSend}'`,
-                commandName,
-                { name: error.name, message: error.message, stack: error.stack },
+              return handleSendError(
+                new OCPPError(
+                  ErrorType.GENERIC_ERROR,
+                  `WebSocket errored for ${
+                    params?.skipBufferingOnError === false ? '' : 'non '
+                  }buffered message id '${messageId}' with content '${messageToSend}'`,
+                  commandName,
+                  { name: error.name, message: error.message, stack: error.stack },
+                ),
               );
-              if (params?.skipBufferingOnError === false) {
-                return bufferAndRejectWithOcppError(ocppError);
-              }
-              return rejectWithOcppError(ocppError);
             }
           });
         } else {
-          const ocppError = new OCPPError(
-            ErrorType.GENERIC_ERROR,
-            `WebSocket closed for ${
-              params?.skipBufferingOnError === false ? '' : 'non '
-            }buffered message id '${messageId}' with content '${messageToSend}'`,
-            commandName,
-            (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
+          return handleSendError(
+            new OCPPError(
+              ErrorType.GENERIC_ERROR,
+              `WebSocket closed for ${
+                params?.skipBufferingOnError === false ? '' : 'non '
+              }buffered message id '${messageId}' with content '${messageToSend}'`,
+              commandName,
+              (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
+            ),
           );
-          if (params?.skipBufferingOnError === false) {
-            return bufferAndRejectWithOcppError(ocppError);
-          }
-          return rejectWithOcppError(ocppError);
         }
       });
     }