/**
* Function that will receive the request's error response
*
- * @param error -
+ * @param ocppError -
* @param requestStatistic -
*/
- const errorCallback = (error: OCPPError, requestStatistic = true): void => {
+ const errorCallback = (ocppError: OCPPError, requestStatistic = true): void => {
if (requestStatistic === true && chargingStation.stationInfo?.enableStatistics === true) {
chargingStation.performanceStatistics?.addRequestStatistic(
commandName,
messageType,
)} command ${commandName} with PDU %j:`,
messagePayload,
- error,
+ ocppError,
);
chargingStation.requests.delete(messageId);
- reject(error);
+ reject(ocppError);
+ };
+
+ const rejectWithOcppError = (ocppError: OCPPError): void => {
+ // Reject response
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ return reject(ocppError);
+ }
+ // Reject and remove request from the cache
+ return errorCallback(ocppError, false);
};
if (chargingStation.stationInfo?.enableStatistics === true) {
if (chargingStation.isWebSocketConnectionOpened() === true) {
const beginId = PerformanceStatistics.beginMeasure(commandName);
const sendTimeout = setTimeout(() => {
- return errorCallback(
+ return rejectWithOcppError(
new OCPPError(
ErrorType.GENERIC_ERROR,
`Timeout for message id '${messageId}'`,
commandName,
(messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT,
),
- false,
);
}, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT);
chargingStation.wsConnection?.send(messageToSend, (error?: Error) => {
// Reject and keep request in the cache
return reject(ocppError);
}
- // Reject response
- if (messageType !== MessageType.CALL_MESSAGE) {
- return reject(ocppError);
- }
- // Reject and remove request from the cache
- return errorCallback(ocppError, false);
+ return rejectWithOcppError(ocppError);
}
});
if (messageType === MessageType.CALL_MESSAGE) {
);
PerformanceStatistics.endMeasure(commandName, beginId);
} 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,
+ );
if (params?.skipBufferingOnError === false) {
// Buffer
chargingStation.bufferMessage(messageToSend);
errorCallback,
);
}
+ // Reject and keep request in the cache
+ return reject(ocppError);
}
- // Reject and keep request in the cache
- return reject(
- 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 rejectWithOcppError(ocppError);
}
// Resolve response
if (messageType !== MessageType.CALL_MESSAGE) {