Permalink
Browse files

Try-catch around message deserialization

  • Loading branch information...
1 parent c846aff commit 0ea9664a0cf9770cd4a969ab140abfd1f9a4c9be @christav christav committed Mar 15, 2013
Showing with 26 additions and 8 deletions.
  1. +16 −8 lib/servicebusconnector.js
  2. +10 −0 test/servicebusconnector-tests.js
@@ -51,7 +51,9 @@ ServiceBusConnector.prototype.start = function () {
if (!err) {
var msg = self.unpackMessage(receivedMessage);
- self.emit('message', msg.nodeId, msg.name, msg.args, msg.seq);
+ if (msg !== null) {
+ self.emit('message', msg.nodeId, msg.name, msg.args, msg.seq);
+ }
}
if (!self.shouldStop) {
@@ -93,16 +95,22 @@ ServiceBusConnector.prototype.packMessage = function(name, args) {
brokerProperties: {
CorrelationId: this.nodeId,
Label: name
- }};
+ }
+ };
}
ServiceBusConnector.prototype.unpackMessage = function(message) {
- return {
- name: message.brokerProperties.Label,
- nodeId: message.brokerProperties.CorrelationId,
- args: JSON.parse(message.body),
- seq: +message.brokerProperties.SequenceNumber
- };
+ try {
+ return {
+ name: message.brokerProperties.Label,
+ nodeId: message.brokerProperties.CorrelationId,
+ args: JSON.parse(message.body),
+ seq: +message.brokerProperties.SequenceNumber
+ };
+ } catch (ex) {
+ // Issue unpacking the message, assume it's bad and toss it
+ return null;
+ }
}
function createRetryFilter(options) {
@@ -208,6 +208,16 @@ describe('Service Bus connection layer', function () {
sb.receiveSubscriptionMessage.calledTwice.should.be.true;
done();
});
+
+ it('should not raise event and repoll on undeserializable message', function (done) {
+ connector.on('message', function (nodeId, name, args, seq) {
+ done(new Error('Message received when deserialization fails. This should not happen.'));
+ });
+
+ receive(null, 'This is not valid JSON');
+ sb.receiveSubscriptionMessage.calledTwice.should.be.true;
+ done();
+ });
});
describe('when receiving with multiple receives at a time', function () {

0 comments on commit 0ea9664

Please sign in to comment.