From 3c80de96aa61159bf5551c816264e878f2b84e35 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 19 Nov 2023 17:11:25 +0100 Subject: [PATCH] refactor: factor out WS error handling code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/OCPPRequestService.ts | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 0d59131f..25305ca1 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -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); } }); } -- 2.34.1