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 () => {