Skip to content

Commit 90d36a7

Browse files
committed
fix: validation errors should be returned when receiver in strictMode
1 parent 9ff053f commit 90d36a7

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ class RPCClient extends EventEmitter {
723723
validator.validate(`urn:${method}.req`, params);
724724
} catch (error) {
725725
this.emit('strictValidationFailure', error);
726-
throw createRPCError("InternalError");
726+
throw error;
727727
}
728728
}
729729

test/client.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const assert = require('assert/strict');
22
const http = require('http');
33
const { once } = require('events');
44
const RPCClient = require("../lib/client");
5-
const { TimeoutError, RPCFrameworkError, RPCError, RPCProtocolError, RPCTypeConstraintViolationError, RPCOccurenceConstraintViolationError, RPCPropertyConstraintViolationError } = require('../lib/errors');
5+
const { TimeoutError, RPCFrameworkError, RPCError, RPCProtocolError, RPCTypeConstraintViolationError, RPCOccurenceConstraintViolationError, RPCPropertyConstraintViolationError, RPCOccurrenceConstraintViolationError, RPCFormationViolationError } = require('../lib/errors');
66
const RPCServer = require("../lib/server");
77
const { setTimeout } = require('timers/promises');
88
const { createValidator } = require('../lib/validator');
@@ -1050,7 +1050,7 @@ describe('RPCClient', function(){
10501050

10511051
describe('#call', function() {
10521052

1053-
it("should reject with 'RPCError' after invalid payload with strictMode", async () => {
1053+
it("should reject with 'RPCError' after invalid payload with client strictMode", async () => {
10541054

10551055
const {endpoint, close, server} = await createServer({
10561056
protocols: ['echo1.0']
@@ -1088,6 +1088,46 @@ describe('RPCClient', function(){
10881088

10891089
});
10901090

1091+
it("should reject with 'RPCError' after invalid payload with server strictMode", async () => {
1092+
1093+
const {endpoint, close, server} = await createServer({
1094+
protocols: ['ocpp1.6'],
1095+
strictMode: true,
1096+
}, {withClient: cli => {
1097+
cli.handle(() => {});
1098+
}});
1099+
const cli = new RPCClient({
1100+
endpoint,
1101+
identity: 'X',
1102+
protocols: ['ocpp1.6'],
1103+
});
1104+
1105+
try {
1106+
await cli.connect();
1107+
1108+
const [c1, c2, c3] = await Promise.allSettled([
1109+
cli.call('UpdateFirmware', {}),
1110+
cli.call('Heartbeat', {a:123}),
1111+
cli.call('UpdateFirmware', {location: "a", retrieveDate: "a"}),
1112+
]);
1113+
1114+
assert.equal(c1.status, 'rejected');
1115+
assert.equal(c1.reason.rpcErrorCode, 'OccurrenceConstraintViolation');
1116+
assert.ok(c1.reason instanceof RPCOccurrenceConstraintViolationError);
1117+
assert.equal(c2.status, 'rejected');
1118+
assert.equal(c2.reason.rpcErrorCode, 'PropertyConstraintViolation');
1119+
assert.ok(c2.reason instanceof RPCPropertyConstraintViolationError);
1120+
assert.equal(c3.status, 'rejected');
1121+
assert.equal(c3.reason.rpcErrorCode, 'FormationViolation');
1122+
assert.ok(c3.reason instanceof RPCFormationViolationError);
1123+
1124+
} finally {
1125+
await cli.close();
1126+
close();
1127+
}
1128+
1129+
});
1130+
10911131

10921132
it("should reject with 'RPCProtocolError' after invalid response with strictMode", async () => {
10931133
const {endpoint, close, server} = await createServer({

0 commit comments

Comments
 (0)