Skip to content

Commit

Permalink
fix: EOCI-213 [pt. 4] better error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aecorredor committed Oct 3, 2024
1 parent e888a99 commit dda72ab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
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

0 comments on commit dda72ab

Please sign in to comment.