Skip to content

Commit

Permalink
refactor: factor out WS error handling code
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
Jérôme Benoit committed Nov 19, 2023
1 parent 69dae41 commit 3c80de9
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions src/charging-station/ocpp/OCPPRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit 3c80de9

Please sign in to comment.