Skip to content

Commit

Permalink
Fix: error handling of non Error class instances
Browse files Browse the repository at this point in the history
  • Loading branch information
aradbar committed Jun 5, 2023
1 parent 87fd299 commit df2238a
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,15 @@ export class Consumer extends TypedEventEmitter {

return result instanceof Object ? result : message;
} catch (err) {
err.message =
err instanceof TimeoutError
? `Message handler timed out after ${this.handleMessageTimeout}ms: Operation timed out.`
: `Unexpected message handler failure: ${err.message}`;
if (!(err instanceof Error)) throw err;
try {
err.message =
err instanceof TimeoutError
? `Message handler timed out after ${this.handleMessageTimeout}ms: Operation timed out.`
: `Unexpected message handler failure: ${err.message}`;
} catch {
/** empty **/
}
throw err;
} finally {
if (handleMessageTimeoutId) {
Expand All @@ -456,7 +461,12 @@ export class Consumer extends TypedEventEmitter {

return result instanceof Object ? result : messages;
} catch (err) {
err.message = `Unexpected message handler failure: ${err.message}`;
if (!(err instanceof Error)) throw err;
try {
err.message = `Unexpected message handler failure: ${err.message}`;
} catch {
/** empty **/
}
throw err;
}
}
Expand Down
130 changes: 130 additions & 0 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,57 @@ describe('Consumer', () => {
);
});

it('handles non-standard exceptions thrown by the handler function', async () => {
class CustomError extends Error {
private _message: string;

constructor(message) {
super();
this._message = message;
}

get message() {
return this._message;
}
}

consumer = new Consumer({
queueUrl: QUEUE_URL,
region: REGION,
handleMessage: () => {
throw new CustomError('unexpected parsing error');
},
sqs,
authenticationErrorTimeout: AUTHENTICATION_ERROR_TIMEOUT
});

consumer.start();
const err: any = await pEvent(consumer, 'processing_error');
consumer.stop();

assert.ok(err);
assert.equal(err.message, 'unexpected parsing error');
});

it('handles non-errors thrown by the handler function', async () => {
consumer = new Consumer({
queueUrl: QUEUE_URL,
region: REGION,
handleMessage: () => {
throw { message: 'unexpected error' };
},
sqs,
authenticationErrorTimeout: AUTHENTICATION_ERROR_TIMEOUT
});

consumer.start();
const err: any = await pEvent(consumer, 'processing_error');
consumer.stop();

assert.ok(err);
assert.equal(err.message, 'unexpected error');
});

it('fires an error event when an error occurs deleting a message', async () => {
const deleteErr = new Error('Delete error');

Expand Down Expand Up @@ -742,6 +793,85 @@ describe('Consumer', () => {
sandbox.assert.callCount(handleMessageBatch, 1);
});

it('handles unexpected exceptions thrown by the handler batch function', async () => {
consumer = new Consumer({
queueUrl: QUEUE_URL,
messageAttributeNames: ['attribute-1', 'attribute-2'],
region: REGION,
handleMessageBatch: () => {
throw new Error('unexpected parsing error');
},
batchSize: 2,
sqs,
authenticationErrorTimeout: AUTHENTICATION_ERROR_TIMEOUT
});

consumer.start();
const err: any = await pEvent(consumer, 'error');
consumer.stop();

assert.ok(err);
assert.equal(
err.message,
'Unexpected message handler failure: unexpected parsing error'
);
});

it('handles non-standard exceptions thrown by the handler batch function', async () => {
class CustomError extends Error {
private _message: string;

constructor(message) {
super();
this._message = message
}

get message() {
return this._message;
}
}

consumer = new Consumer({
queueUrl: QUEUE_URL,
messageAttributeNames: ['attribute-1', 'attribute-2'],
region: REGION,
handleMessageBatch: () => {
throw new CustomError('unexpected parsing error');
},
batchSize: 2,
sqs,
authenticationErrorTimeout: AUTHENTICATION_ERROR_TIMEOUT
});

consumer.start();
const err: any = await pEvent(consumer, 'error');
consumer.stop();

assert.ok(err);
assert.equal(err.message, 'unexpected parsing error');
});

it('handles non-errors thrown by the handler batch function', async () => {
consumer = new Consumer({
queueUrl: QUEUE_URL,
messageAttributeNames: ['attribute-1', 'attribute-2'],
region: REGION,
handleMessageBatch: () => {
throw { message: 'unexpected error' };
},
batchSize: 2,
sqs,
authenticationErrorTimeout: AUTHENTICATION_ERROR_TIMEOUT
});

consumer.start();
const err: any = await pEvent(consumer, 'error');
consumer.stop();

assert.ok(err);
assert.equal(err.message, 'unexpected error');
});

it('prefers handleMessagesBatch over handleMessage when both are set', async () => {
consumer = new Consumer({
queueUrl: QUEUE_URL,
Expand Down

0 comments on commit df2238a

Please sign in to comment.