Skip to content

Commit

Permalink
chore: formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlavin committed Feb 15, 2024
1 parent d509ed9 commit 8aa3303
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
50 changes: 28 additions & 22 deletions src/sqs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,49 +102,55 @@ describe('SQSMessageHandler', () => {
);

// Assert that the message body was redacted.
expect(logger.info).toHaveBeenCalledWith({
event: { Records: [{ attributes: {}, body: 'REDACTED' }] },
}, "Processing SQS topic message")
expect(logger.info).toHaveBeenCalledWith(
{
event: { Records: [{ attributes: {}, body: 'REDACTED' }] },
},
'Processing SQS topic message',
);
});

test('if redaction fails log the message', async () => {
expect.assertions(4);

const error = new Error('Failed to redact message')
const error = new Error('Failed to redact message');
const lambda = new SQSMessageHandler({
logger,
redactMessageBody: () => { throw error },
redactMessageBody: () => {
throw error;
},
parseMessage: testSerializer.parseMessage,
createRunContext: (ctx) => {
expect(typeof ctx.correlationId === 'string').toBe(true);
return {};
},
}).lambda();

const body = JSON.stringify({ data: 'test-event-1' })
const body = JSON.stringify({ data: 'test-event-1' });
const event = {
Records: [
{ attributes: {}, body },
],
} as any
const response = await lambda(
event,
{} as any,
);
Records: [{ attributes: {}, body }],
} as any;
const response = await lambda(event, {} as any);

// Expect no failure
expect(response).toBeUndefined();

// Assert that the message body was shown unredacted. Logging the
// Assert that the message body was shown unredacted. Logging the
// unredacted error allows for easier debugging. Leaking a small amount of
// PHI to Sumo is better than having a system that cannot be debugged.
expect(logger.error).toHaveBeenCalledWith({
error,
body,
}, "Failed to redact message body")
expect(logger.info).toHaveBeenCalledWith({
event,
}, "Processing SQS topic message")
expect(logger.error).toHaveBeenCalledWith(
{
error,
body,
},
'Failed to redact message body',
);
expect(logger.info).toHaveBeenCalledWith(
{
event,
},
'Processing SQS topic message',
);
});

describe('error handling', () => {
Expand Down
47 changes: 27 additions & 20 deletions src/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export type SQSMessageHandlerConfig<Message, Context> =
* default, the full message body is logged.
*/
redactMessageBody?: (body: string) => string;

};

export type SQSMessageAction<Message, Context> = (
Expand Down Expand Up @@ -65,23 +64,24 @@ export type SQSPartialBatchResponse = {
}[];
};

const safeRedactor = (logger: LoggerInterface, redactor: (body: string) => string) =>
const safeRedactor =
(logger: LoggerInterface, redactor: (body: string) => string) =>
(body: string) => {
try {
return redactor(body);
} catch (error) {
logger.error({ error, body }, 'Failed to redact message body');
return body;
}
}
};

/**
* An abstraction for an SQS message handler.
*/
export class SQSMessageHandler<Message, Context> {
private messageActions: SQSMessageAction<Message, Context>[] = [];

constructor(readonly config: SQSMessageHandlerConfig<Message, Context>) { }
constructor(readonly config: SQSMessageHandlerConfig<Message, Context>) {}

/**
* Adds a message action to the handler.
Expand Down Expand Up @@ -113,15 +113,22 @@ export class SQSMessageHandler<Message, Context> {
Object.assign(context, await this.config.createRunContext(context));

// 2. Process all the records.
const redactor = this.config.redactMessageBody ? safeRedactor(context.logger, this.config.redactMessageBody) : undefined;
const redactedEvent = redactor ? {
...event,
Records: event.Records.map((record) => ({
...record,
body: redactor(record.body),
})),
} : event;
context.logger.info({ event: redactedEvent }, 'Processing SQS topic message');
const redactor = this.config.redactMessageBody
? safeRedactor(context.logger, this.config.redactMessageBody)
: undefined;
const redactedEvent = redactor
? {
...event,
Records: event.Records.map((record) => ({
...record,
body: redactor(record.body),
})),
}
: event;
context.logger.info(
{ event: redactedEvent },
'Processing SQS topic message',
);

const processingResult = await processWithOrdering(
{
Expand Down Expand Up @@ -208,13 +215,13 @@ export class SQSMessageHandler<Message, Context> {
const event: SQSEvent = {
Records: messages.map(
(msg) =>
// We don't need to mock every field on this event -- there are lots.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
({
attributes: {},
messageId: uuid(),
body: stringifyMessage(msg),
} as any),
// We don't need to mock every field on this event -- there are lots.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
({
attributes: {},
messageId: uuid(),
body: stringifyMessage(msg),
} as any),
),
};

Expand Down

0 comments on commit 8aa3303

Please sign in to comment.