Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two differents errors events. 'error' is for specific AWS clien… #20

Merged
merged 2 commits into from
Dec 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Each consumer is an [`EventEmitter`](http://nodejs.org/api/events.html) and emit

|Event|Params|Description|
|-----|------|-----------|
|`error`|`err`|Fired when an error occurs interacting with the queue or processing the message.|
|`error`|`err`|Fired when an error occurs interacting with the queue.|
|`processing_error`|`err`|Fired when an error occurs processing the message.|
|`message_received`|`message`|Fired when a message is received.|
|`message_processed`|`message`|Fired when a message is successfully processed and removed from the queue.|
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ var requiredOptions = [
'handleMessage'
];

/**
* Construct a new SQSError
*/
function SQSError(message) {
this.name = 'SQSError';
this.message = (message || '');
}
SQSError.prototype = Error.prototype;

function validate(options) {
requiredOptions.forEach(function (option) {
if (!options[option]) {
Expand Down Expand Up @@ -97,7 +106,7 @@ Consumer.prototype._poll = function () {
};

Consumer.prototype._handleSqsResponse = function (err, response) {
if (err) this.emit('error', new Error('SQS receive message failed: ' + err.message));
if (err) this.emit('error', new SQSError('SQS receive message failed: ' + err.message));

var consumer = this;

Expand Down Expand Up @@ -128,11 +137,14 @@ Consumer.prototype._processMessage = function (message, cb) {
}
], function (err) {
if (err) {
consumer.emit('error', err);
if (err.name === 'SQSError') {
consumer.emit('error', err);
} else {
consumer.emit('processing_error', err);
}
} else {
consumer.emit('message_processed', message);
}

cb();
});
};
Expand All @@ -145,7 +157,7 @@ Consumer.prototype._deleteMessage = function (message, cb) {

debug('Deleting message %s', message.MessageId);
this.sqs.deleteMessage(deleteParams, function (err) {
if (err) return cb(new Error('SQS delete message failed: ' + err.message));
if (err) return cb(new SQSError('SQS delete message failed: ' + err.message));

cb();
});
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('Consumer', function () {

handleMessage.yields(processingErr);

consumer.on('error', function (err) {
consumer.on('processing_error', function (err) {
assert.equal(err, processingErr);
done();
});
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('Consumer', function () {
it('doesn\'t delete the message when a processing error is reported', function () {
handleMessage.yields(new Error('Processing error'));

consumer.on('error', function () {
consumer.on('processing_error', function () {
// ignore the error
});

Expand Down