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

EOCI-213: [pt. 4] better error logging #80

Merged
merged 1 commit into from
Oct 3, 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
4 changes: 2 additions & 2 deletions src/dynamo-streams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ describe('DynamoStreamHandler', () => {
// Expect that redaction is working
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'one',
itemIdentifier: 'one',
failedRecord: {
eventName: 'INSERT',
dynamodb: {
Expand All @@ -926,7 +926,7 @@ describe('DynamoStreamHandler', () => {
// expect that third event is logged
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'three',
itemIdentifier: 'three',
failedRecord: {
eventName: 'INSERT',
dynamodb: {
Expand Down
4 changes: 2 additions & 2 deletions src/kinesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ describe('KinesisEventHandler', () => {
// Expect that first event is logged
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'one',
itemIdentifier: 'one',
failedRecord: {
kinesis: {
sequenceNumber: 'one',
Expand All @@ -559,7 +559,7 @@ describe('KinesisEventHandler', () => {
// Expect that third event is logged
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'three',
itemIdentifier: 'three',
failedRecord: {
kinesis: {
sequenceNumber: 'three',
Expand Down
8 changes: 4 additions & 4 deletions src/sqs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ describe('SQSMessageHandler', () => {
// First failure group, expecting message bodies
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'message-3',
itemIdentifier: 'message-3',
failedRecord: expect.objectContaining({
body: JSON.stringify({
name: `test-event-3`,
Expand All @@ -387,7 +387,7 @@ describe('SQSMessageHandler', () => {
// Second failure group, expecting message bodies
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'message-7',
itemIdentifier: 'message-7',
failedRecord: expect.objectContaining({
body: JSON.stringify({
name: `test-event-7`,
Expand Down Expand Up @@ -487,7 +487,7 @@ describe('SQSMessageHandler', () => {
// First failure group, expecting message bodies are redacted
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'message-3',
itemIdentifier: 'message-3',
failedRecord: expect.objectContaining({
body: 'REDACTED',
messageId: 'message-3',
Expand All @@ -502,7 +502,7 @@ describe('SQSMessageHandler', () => {
// Second failure group, expecting message bodies are redacted
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
identifier: 'message-7',
itemIdentifier: 'message-7',
failedRecord: expect.objectContaining({
body: 'REDACTED',
messageId: 'message-7',
Expand Down
25 changes: 14 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,32 @@ export const handleUnprocessedRecords = <Record>(params: {
return;
}

if (!params.usePartialBatchResponses) {
throw new AggregateError(
params.unprocessedRecords.filter((i) => 'error' in i).map((e) => e.error),
);
}

// Log all the failures.
const batchItemFailures: { itemIdentifier: string }[] = [];
const errors: any[] = [];
// Even when not using partial batch responses, we still want to log all
// the errors before throwing so we can easily correlate errors by the
// logger correlation ID, request ID, and event ID.
for (const { item, error } of params.unprocessedRecords) {
const itemIdentifier = params.getItemIdentifier(item);
batchItemFailures.push({ itemIdentifier });

if (error) {
errors.push(error);
params.logger.error(
{
err: error,
identifier: params.getItemIdentifier(item),
itemIdentifier,
failedRecord: params.redactRecord ? params.redactRecord(item) : item,
usePartialBatchResponses: params.usePartialBatchResponses,
},
'Failed to process record',
);
}
}

const batchItemFailures = params.unprocessedRecords.map(({ item }) => ({
itemIdentifier: params.getItemIdentifier(item),
}));
if (!params.usePartialBatchResponses) {
throw new AggregateError(errors);
}

params.logger.info(
{ batchItemFailures },
Expand Down
Loading