import { ClientRequestArgs } from 'http';
import Configuration from '../utils/Configuration';
import Constants from '../utils/Constants';
+import { ErrorType } from '../types/ocpp/ErrorType';
import FileUtils from '../utils/FileUtils';
import { MessageType } from '../types/ocpp/MessageType';
import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService';
import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService';
import OCPP16ResponseService from './ocpp/1.6/OCPP16ResponseService';
-import OCPPError from './OCPPError';
+import OCPPError from './ocpp/OCPPError';
import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
import OCPPRequestService from './ocpp/OCPPRequestService';
import { OCPPVersion } from '../types/ocpp/OCPPVersion';
// Parse the message
[messageType, messageId, commandName, commandPayload, errorDetails] = request;
} else {
- throw new Error('Incoming request is not iterable');
+ throw new OCPPError(ErrorType.PROTOCOL_ERROR, 'Incoming request is not iterable');
}
// Check the Type of message
switch (messageType) {
if (Utils.isIterable(this.requests[messageId])) {
[responseCallback, , requestPayload] = this.requests[messageId];
} else {
- throw new Error(`Response request for message id ${messageId} is not iterable`);
+ throw new OCPPError(ErrorType.PROTOCOL_ERROR, `Response request for message id ${messageId} is not iterable`);
}
if (!responseCallback) {
// Error
- throw new Error(`Response request for unknown message id ${messageId}`);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, `Response request for unknown message id ${messageId}`);
}
delete this.requests[messageId];
responseCallback(commandName, requestPayload);
case MessageType.CALL_ERROR_MESSAGE:
if (!this.requests[messageId]) {
// Error
- throw new Error(`Error request for unknown message id ${messageId}`);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, `Error request for unknown message id ${messageId}`);
}
if (Utils.isIterable(this.requests[messageId])) {
[, rejectCallback] = this.requests[messageId];
} else {
- throw new Error(`Error request for message id ${messageId} is not iterable`);
+ throw new OCPPError(ErrorType.PROTOCOL_ERROR, `Error request for message id ${messageId} is not iterable`);
}
delete this.requests[messageId];
rejectCallback(new OCPPError(commandName, commandPayload.toString(), errorDetails));
default:
errMsg = `${this.logPrefix()} Wrong message type ${messageType}`;
logger.error(errMsg);
- throw new Error(errMsg);
+ throw new OCPPError(ErrorType.PROTOCOL_ERROR, errMsg);
}
} catch (error) {
// Log
+++ /dev/null
-import { ErrorType } from '../types/ocpp/ErrorType';
-
-export default class OCPPError extends Error {
- code: string;
- details?: any;
-
- constructor(code: string, message: string, details?: any) {
- super(message);
-
- this.code = code ?? ErrorType.GENERIC_ERROR;
- this.message = message ?? '';
- this.details = details ?? {};
-
- Object.setPrototypeOf(this, OCPPError.prototype); // For instanceof
-
- Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : (this.stack = (new Error()).stack);
- }
-}
import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
-import OCPPError from '../../OCPPError';
+import OCPPError from '../OCPPError';
import OCPPIncomingRequestService from '../OCPPIncomingRequestService';
import Utils from '../../../utils/Utils';
import fs from 'fs';
}
} else {
// Throw exception
- await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented`, {}), commandName);
- throw new Error(`${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload)}`);
+ const errMsg = `${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload, null, 2)}`;
+ await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg), commandName);
+ throw new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg);
}
// Send the built response
await this.chargingStation.ocppRequestService.sendMessage(messageId, response, MessageType.CALL_RESULT_MESSAGE, commandName);
}
return { fileName: diagnosticsArchive };
}
- throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
+ throw new OCPPError(ErrorType.GENERIC_ERROR, `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
}
- throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
+ throw new OCPPError(ErrorType.GENERIC_ERROR, `Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`);
} catch (error) {
await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed);
if (ftpClient) {
import { MeterValueUnit, MeterValuesRequest, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase } from '../../../types/ocpp/1.6/MeterValues';
import Constants from '../../../utils/Constants';
+import { ErrorType } from '../../../types/ocpp/ErrorType';
import MeasurandPerPhaseSampledValueTemplates from '../../../types/MeasurandPerPhaseSampledValueTemplates';
import MeasurandValues from '../../../types/MeasurandValues';
import { MessageType } from '../../../types/ocpp/MessageType';
import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
-import OCPPError from '../../OCPPError';
+import OCPPError from '../OCPPError';
import OCPPRequestService from '../OCPPRequestService';
import Utils from '../../../utils/Utils';
import logger from '../../../utils/Logger';
break;
default:
logger.error(errMsg);
- throw new Error(errMsg);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg);
}
meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(powerSampledValueTemplate, powerMeasurandValues.allPhases));
const sampledValuesIndex = meterValue.sampledValue.length - 1;
break;
default:
logger.error(errMsg);
- throw new Error(errMsg);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg);
}
meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(currentSampledValueTemplate, currentMeasurandValues.allPhases));
const sampledValuesIndex = meterValue.sampledValue.length - 1;
import { MeterValueContext, MeterValueLocation, MeterValueUnit, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase, OCPP16SampledValue } from '../../../types/ocpp/1.6/MeterValues';
import ChargingStation from '../../ChargingStation';
+import { ErrorType } from '../../../types/ocpp/ErrorType';
+import OCPPError from '../OCPPError';
import { SampledValueTemplate } from '../../../types/Connectors';
import Utils from '../../../utils/Utils';
import logger from '../../../utils/Logger';
if (Utils.isUndefined(chargingStation.stationInfo.powerDivider)) {
const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider is undefined`;
logger.error(errMsg);
- throw new Error(errMsg);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg);
} else if (chargingStation.stationInfo?.powerDivider <= 0) {
const errMsg = `${chargingStation.logPrefix()} MeterValues measurand ${measurandType ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: powerDivider have zero or below value ${chargingStation.stationInfo.powerDivider}`;
logger.error(errMsg);
- throw new Error(errMsg);
+ throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg);
}
}
--- /dev/null
+import { ErrorType } from '../../types/ocpp/ErrorType';
+import { IncomingRequestCommand } from '../../types/ocpp/Requests';
+
+export default class OCPPError extends Error {
+ code: ErrorType | IncomingRequestCommand;
+ details?: unknown;
+
+ constructor(code: ErrorType | IncomingRequestCommand, message: string, details?: unknown) {
+ super(message);
+
+ this.name = new.target.name;
+ this.code = code ?? ErrorType.GENERIC_ERROR;
+ this.message = message ?? '';
+ this.details = details ?? {};
+
+ Object.setPrototypeOf(this, new.target.prototype);
+
+ Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : (this.stack = (new Error()).stack);
+ }
+}
import { ErrorType } from '../../types/ocpp/ErrorType';
import { MessageType } from '../../types/ocpp/MessageType';
import { MeterValue } from '../../types/ocpp/MeterValues';
-import OCPPError from '../OCPPError';
+import OCPPError from './OCPPError';
import OCPPResponseService from './OCPPResponseService';
import PerformanceStatistics from '../../utils/PerformanceStatistics';
import logger from '../../utils/Logger';
import { MessageType } from './MessageType';
import { OCPP16DiagnosticsStatus } from './1.6/DiagnosticsStatus';
-import OCPPError from '../../charging-station/OCPPError';
+import OCPPError from '../../charging-station/ocpp/OCPPError';
export default interface Requests {
[id: string]: Request;