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

Attach failed message ids to error object #100

Merged
merged 3 commits into from
Mar 11, 2024
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
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
Loading