Catch promise reject on CheckOfflineChargingStationsTask
MODIFY
src/client/ocpp/json/JsonRestChargingStationClient.ts
src/client/ocpp/json/JsonRestChargingStationClient.ts
@@ -101,7 +101,7 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
return this.sendMessage(this.buildRequest(Command.TRIGGER_DATA_TRANSFER, params));
}
- private async openConnection(): Promise<unknown> {
+ private async openConnection(): Promise<any> {
// Log
await Logging.logInfo({
tenantID: this.tenantID,
@@ -114,10 +114,9 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
module: MODULE_NAME, method: 'onOpen',
message: `Try to connect to '${this.serverURL}' ${Configuration.isCloudFoundry() ? ', CF Instance \'' + this.chargingStation.cfApplicationIDAndInstanceIndex + '\'' : ''}`
});
-
// Create Promise
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
return await new Promise((resolve, reject) => {
+ try {
// Create WS
let WSOptions = {};
if (Configuration.isCloudFoundry()) {
@@ -139,7 +138,6 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
this.wsConnection = new WSClient(this.serverURL, wsClientOptions);
// Opened
this.wsConnection.onopen = async () => {
- // Log
await Logging.logInfo({
tenantID: this.tenantID,
siteID: this.chargingStation.siteID,
@@ -156,7 +154,6 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
};
// Closed
this.wsConnection.onclose = async (code: number) => {
- // Log
await Logging.logInfo({
tenantID: this.tenantID,
siteID: this.chargingStation.siteID,
@@ -172,7 +169,6 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
};
// Handle Error Message
this.wsConnection.onerror = async (error: Error) => {
- // Log
await Logging.logError({
tenantID: this.tenantID,
siteID: this.chargingStation.siteID,
@@ -187,6 +183,7 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
});
// Terminate WS in error
this.terminateConnection();
+ reject(new Error(`Error on opening Web Socket connection: ${error.message}'`));
};
// Handle Server Message
this.wsConnection.onmessage = async (message) => {
@@ -234,7 +231,6 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
});
}
} catch (error) {
- // Log
await Logging.logException(
error,
ServerAction.WS_REST_CLIENT_MESSAGE,
@@ -244,6 +240,9 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
);
}
};
+ } catch (error) {
+ reject(new Error(`Unexpected error on opening Web Socket connection: ${error.message}'`));
+ }
});
}
@@ -266,7 +265,8 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
private async sendMessage(request: OCPPOutgoingRequest): Promise<any> {
// Return a promise
// eslint-disable-next-line @typescript-eslint/no-misused-promises, no-async-promise-executor
- const promise = await new Promise(async (resolve, reject) => {
+ return new Promise(async (resolve, reject) => {
+ try {
// Open WS Connection
await this.openConnection();
// Check if wsConnection is ready
@@ -277,10 +277,12 @@ export default class JsonRestChargingStationClient extends ChargingStationClient
this.requests[request[1]] = { resolve, reject, command: request[2] };
} else {
// Reject it
- return reject(`Socket is closed for message ${request[2]}`);
+ reject(new Error(`Socket is closed for message ${request[2]}`));
+ }
+ } catch (error) {
+ reject(new Error(`Unexpected error on request '${request[2]}': ${error.message}'`));
}
});
- return promise;
}
private buildRequest(command, params = {}): OCPPOutgoingRequest {

Catch promise reject on CheckOfflineChargingStationsTask