Fixed handling of error in OCPP request service endpoint
MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -1270,7 +1270,7 @@ export default class ChargingStationService {
}
let filteredRequest: any;
try {
- let result: any;
+ let result: any = {};
switch (command) {
// Clear Cache
case Command.CLEAR_CACHE:
@@ -1345,29 +1345,36 @@ export default class ChargingStationService {
filteredRequest = ChargingStationValidator.getInstance().validateChargingStationActionResetReq(req.body);
result = await chargingStationClient.reset({ type: filteredRequest.args.type });
break;
- // Other commands
+ // Unknwon command
default:
- // Log Specific Schema has not been verified:
- await Logging.logError({
- tenantID: req.tenant.id,
- siteID: chargingStation.siteID,
+ throw new AppError({
+ chargingStationID: chargingStation.id,
siteAreaID: chargingStation.siteAreaID,
+ siteID: chargingStation.siteID,
companyID: chargingStation.companyID,
- chargingStationID: chargingStation.id,
- user: req.user,
action: action,
- module: MODULE_NAME, method: 'handleAction',
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
- message: `Command '${command}' has not its own validation schema.`
+ errorCode: HTTPError.GENERAL_ERROR,
+ message: `Unknown OCPP command '${command}'`,
+ module: MODULE_NAME,
+ method: 'handleAction',
+ user: req.user,
});
+ }
+ // Expect result
+ if (!result) {
throw new AppError({
+ chargingStationID: chargingStation.id,
+ siteAreaID: chargingStation.siteAreaID,
+ siteID: chargingStation.siteID,
+ companyID: chargingStation.companyID,
+ action: action,
errorCode: HTTPError.GENERAL_ERROR,
- message: `Command '${command}' has not its own validation schema.`,
+ message: `Received an empty response from OCPP command '${command}'`,
module: MODULE_NAME,
- method: 'handleAction'
+ method: 'handleAction',
+ user: req.user,
});
}
- if (result) {
// OCPP Command with status
if (Utils.objectHasProperty(result, 'status') && ![OCPPStatus.ACCEPTED, OCPPUnlockStatus.UNLOCKED].includes(result.status)) {
await Logging.logError({
@@ -1377,7 +1384,7 @@ export default class ChargingStationService {
companyID: chargingStation.companyID,
chargingStationID: chargingStation.id,
user: req.user,
- module: MODULE_NAME, method: 'handleChargingStationCommand',
+ module: MODULE_NAME, method: 'handleAction',
action: action,
message: `OCPP Command '${command}' has failed`,
detailedMessages: { filteredRequest, result }
@@ -1391,7 +1398,7 @@ export default class ChargingStationService {
companyID: chargingStation.companyID,
chargingStationID: chargingStation.id,
user: req.user,
- module: MODULE_NAME, method: 'handleChargingStationCommand',
+ module: MODULE_NAME, method: 'handleAction',
action: action,
message: `OCPP Command '${command}' has been executed successfully`,
detailedMessages: { filteredRequest, result }
@@ -1399,21 +1406,6 @@ export default class ChargingStationService {
}
res.json(result);
next();
- return;
- }
- // Throw error
- throw new AppError({
- chargingStationID: chargingStation.id,
- siteAreaID: chargingStation.siteAreaID,
- siteID: chargingStation.siteID,
- companyID: chargingStation.companyID,
- action: action,
- errorCode: HTTPError.GENERAL_ERROR,
- message: `Unknown OCPP command '${command}'`,
- module: MODULE_NAME,
- method: 'handleChargingStationCommand',
- user: req.user,
- });
} catch (error) {
throw new AppError({
chargingStationID: chargingStation.id,
@@ -1423,7 +1415,7 @@ export default class ChargingStationService {
action: action,
errorCode: HTTPError.GENERAL_ERROR,
message: `OCPP Command '${command}' has failed`,
- module: MODULE_NAME, method: 'handleChargingStationCommand',
+ module: MODULE_NAME, method: 'handleAction',
user: req.user,
detailedMessages: { error: error.stack }
});

Fixed handling of error in OCPP request service endpoint