Improve OCPP request's response error handling
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 4996b9e7aa89beb9f075cc7063898b0045757be5..2076a2964a061b3c4767208d973e872cf0558d97 100644 (file)
@@ -12,6 +12,7 @@ import { MeterValue } from '../../types/ocpp/MeterValues';
 import OCPPError from './OCPPError';
 import OCPPResponseService from './OCPPResponseService';
 import PerformanceStatistics from '../../performance/PerformanceStatistics';
+import Utils from '../../utils/Utils';
 import logger from '../../utils/Logger';
 
 export default abstract class OCPPRequestService {
@@ -28,7 +29,7 @@ export default abstract class OCPPRequestService {
     // eslint-disable-next-line @typescript-eslint/no-this-alias
     const self = this;
     // Send a message through wsConnection
-    return new Promise((resolve, reject) => {
+    return Utils.promiseWithTimeout(new Promise((resolve, reject) => {
       const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback);
       if (this.chargingStation.getEnableStatistics()) {
         this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
@@ -58,8 +59,6 @@ export default abstract class OCPPRequestService {
         // Yes: send Ok
         return resolve(commandParams);
       }
-      // Send timeout
-      setTimeout(() => rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {}), false), Constants.OCPP_SOCKET_TIMEOUT);
 
       /**
        * Function that will receive the request's response
@@ -71,10 +70,16 @@ export default abstract class OCPPRequestService {
         if (self.chargingStation.getEnableStatistics()) {
           self.chargingStation.performanceStatistics.addRequestStatistic(commandName, MessageType.CALL_RESULT_MESSAGE);
         }
-        // Send the response
-        await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
-        self.chargingStation.requests.delete(messageId);
-        resolve(payload);
+        // Handle the request's response
+        try {
+          await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
+          resolve(payload);
+        } catch (error) {
+          reject(error);
+          throw error;
+        } finally {
+          self.chargingStation.requests.delete(messageId);
+        }
       }
 
       /**
@@ -91,6 +96,8 @@ export default abstract class OCPPRequestService {
         self.chargingStation.requests.delete(messageId);
         reject(error);
       }
+    }), Constants.OCPP_WEBSOCKET_TIMEOUT, new OCPPError(ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandParams?.details ?? {}), () => {
+      messageType === MessageType.CALL_MESSAGE && this.chargingStation.requests.delete(messageId);
     });
   }