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

fix: error handling of non-standard Error instances #405

Merged
merged 6 commits into from
Jun 9, 2023
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
13 changes: 8 additions & 5 deletions src/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,11 @@ 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 TimeoutError) {
err.message = `Message handler timed out after ${this.handleMessageTimeout}ms: Operation timed out.`;
} else if (err instanceof Error) {
err.message = `Unexpected message handler failure: ${err.message}`;
}
throw err;
} finally {
if (handleMessageTimeoutId) {
Expand All @@ -456,7 +457,9 @@ 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) {
err.message = `Unexpected message handler failure: ${err.message}`;
}
throw err;
}
}
Expand Down
88 changes: 88 additions & 0 deletions test/tests/consumer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,37 @@ describe('Consumer', () => {
);
});

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

constructor(message) {
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('fires an error event when an error occurs deleting a message', async () => {
const deleteErr = new Error('Delete error');

Expand Down Expand Up @@ -742,6 +773,63 @@ 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 {
private _message: string;

constructor(message) {
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('prefers handleMessagesBatch over handleMessage when both are set', async () => {
consumer = new Consumer({
queueUrl: QUEUE_URL,
Expand Down