import OCPPError from '../../exception/OCPPError';
import { HandleErrorParams } from '../../types/Error';
import { JsonType } from '../../types/JsonType';
-import { ErrorType } from '../../types/ocpp/ErrorType';
import { IncomingRequestCommand } from '../../types/ocpp/Requests';
import logger from '../../utils/Logger';
import type ChargingStation from '../ChargingStation';
+import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils';
const moduleName = 'OCPPIncomingRequestService';
validate.errors
);
throw new OCPPError(
- ErrorType.FORMATION_VIOLATION,
+ OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors),
'Incoming request PDU is invalid',
commandName,
JSON.stringify(validate.errors, null, 2)
import logger from '../../utils/Logger';
import Utils from '../../utils/Utils';
import type ChargingStation from '../ChargingStation';
+import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils';
import type OCPPResponseService from './OCPPResponseService';
const moduleName = 'OCPPRequestService';
validate.errors
);
throw new OCPPError(
- ErrorType.FORMATION_VIOLATION,
+ OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors),
'Request PDU is invalid',
commandName,
JSON.stringify(validate.errors, null, 2)
import OCPPError from '../../exception/OCPPError';
import { JsonType } from '../../types/JsonType';
-import { ErrorType } from '../../types/ocpp/ErrorType';
import { RequestCommand } from '../../types/ocpp/Requests';
import logger from '../../utils/Logger';
import type ChargingStation from '../ChargingStation';
+import { OCPP16ServiceUtils } from './1.6/OCPP16ServiceUtils';
const moduleName = 'OCPPResponseService';
validate.errors
);
throw new OCPPError(
- ErrorType.FORMATION_VIOLATION,
+ OCPP16ServiceUtils.AjvErrorsToErrorType(validate.errors),
'Response PDU is invalid',
commandName,
JSON.stringify(validate.errors, null, 2)
+import { DefinedError, ErrorObject } from 'ajv';
+
+import { ErrorType } from '../../types/ocpp/ErrorType';
+
export class OCPPServiceUtils {
protected constructor() {
// This is intentional
}
+ public static AjvErrorsToErrorType(errors: ErrorObject[]): ErrorType {
+ for (const error of errors as DefinedError[]) {
+ switch (error.keyword) {
+ case 'type':
+ return ErrorType.TYPE_CONSTRAINT_VIOLATION;
+ case 'dependencies':
+ case 'required':
+ return ErrorType.OCCURRENCE_CONSTRAINT_VIOLATION;
+ case 'pattern':
+ case 'format':
+ return ErrorType.PROPERTY_CONSTRAINT_VIOLATION;
+ }
+ }
+ return ErrorType.FORMAT_VIOLATION;
+ }
+
protected static getLimitFromSampledValueTemplateCustomValue(
value: string,
limit: number,