Skip to content

Commit

Permalink
Merge pull request #80 from bbc/unexpectedExceptions
Browse files Browse the repository at this point in the history
Fix: Catch exceptions from handler and propagate err via callback.
  • Loading branch information
Nick Spragg authored Jun 15, 2017
2 parents 042f796 + 5d76ddb commit 6e3bdc1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ Consumer.prototype._processMessage = function (message, cb) {
this.emit('message_received', message);
async.series([
function handleMessage(done) {
consumer.handleMessage(message, done);
try {
consumer.handleMessage(message, done);
} catch (err) {
done(new Error('Unexpected message handler failure: ' + err.message));
}
},
function deleteMessage(done) {
consumer._deleteMessage(message, done);
Expand All @@ -169,7 +173,7 @@ Consumer.prototype._processMessage = function (message, cb) {
QueueUrl: consumer.queueUrl,
ReceiptHandle: message.ReceiptHandle,
VisibilityTimeout: 0
}, function(err) {
}, function (err) {
if (err) consumer.emit('error', err, message);
cb();
});
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ describe('Consumer', function () {
consumer.start();
});

it('handles unexpected exceptions thrown by the handler function', function (done) {
consumer = new Consumer({
queueUrl: 'some-queue-url',
region: 'some-region',
handleMessage: function () {
throw new Error('unexpected parsing error');
},
sqs: sqs,
authenticationErrorTimeout: 20
});

consumer.on('processing_error', function (err) {
assert.ok(err);
assert.equal(err.message, 'Unexpected message handler failure: unexpected parsing error');
done();
});

consumer.start();
});

it('fires an error event when an error occurs deleting a message', function (done) {
var deleteErr = new Error('Delete error');

Expand Down

0 comments on commit 6e3bdc1

Please sign in to comment.