From 61246de73911d86118f2c02f483328087eaf3b4f Mon Sep 17 00:00:00 2001 From: Kyriakos <627855+knicola@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:48:03 -0400 Subject: [PATCH] attach failed message ids to error object (#100) this is to allow programmatic access to the ids, rather than having to parse the error message. Co-authored-by: Kyriakos Nicola Co-authored-by: Nicholas Griffin --- src/errors.ts | 11 +++++++++++ src/producer.ts | 5 ++--- test/producer.test.ts | 14 ++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 src/errors.ts diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 0000000..c7bc9c3 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,11 @@ +export class FailedMessagesError extends Error { + /** Ids of messages that failed to send. */ + public failedMessages: string[]; + /** + * @param failedMessages Ids of messages that failed to send. + */ + constructor(failedMessages: string[]) { + super(`Failed to send messages: ${failedMessages.join(', ')}`); + this.failedMessages = failedMessages; + } +} diff --git a/src/producer.ts b/src/producer.ts index 9f8d641..fdf47cc 100644 --- a/src/producer.ts +++ b/src/producer.ts @@ -6,6 +6,7 @@ import { } from '@aws-sdk/client-sqs'; import { Message, ProducerOptions } from './types'; import { toEntry } from './format'; +import { FailedMessagesError } from './errors'; const requiredOptions = ['queueUrl']; @@ -104,9 +105,7 @@ export class Producer { if (failedMessagesBatch.length === 0) { return successfulMessagesBatch; } - throw new Error( - `Failed to send messages: ${failedMessagesBatch.join(', ')}` - ); + throw new FailedMessagesError(failedMessagesBatch); } } diff --git a/test/producer.test.ts b/test/producer.test.ts index 36e930b..7d99b86 100644 --- a/test/producer.test.ts +++ b/test/producer.test.ts @@ -455,10 +455,16 @@ describe('Producer', () => { Failed: failedMessages }); - await rejects( - producer.send(['message1', 'message2', 'message3']), - errMessage - ); + try { + await producer.send(['message1', 'message2', 'message3']); + assert.fail('Should have thrown'); + } catch (err) { + assert.equal(err.message, errMessage); + assert.deepEqual( + err.failedMessages, + failedMessages.map((m) => m.Id) + ); + } }); it('returns the approximate size of the queue', async () => {