Skip to content

Commit

Permalink
attach failed message ids to error object (#100)
Browse files Browse the repository at this point in the history
this is to allow programmatic access to the ids,
rather than having to parse the error message.

Co-authored-by: Kyriakos Nicola <[email protected]>
Co-authored-by: Nicholas Griffin <[email protected]>
  • Loading branch information
3 people authored Mar 11, 2024
1 parent d549400 commit 61246de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 2 additions & 3 deletions src/producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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);
}
}

Expand Down
14 changes: 10 additions & 4 deletions test/producer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 61246de

Please sign in to comment.