Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit b6beedf

Browse files
authored
refactor(lambda errors): do not throw errors when no lambda handler (#229)
- list API sends a bunch of events, only one of which we want to handle in this lambda, so we don't want to error on the others.
1 parent 3cf9cfd commit b6beedf

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

lambda/events/index.spec.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@ import { SQSEvent } from 'aws-lambda';
55
import * as Sentry from '@sentry/serverless';
66

77
describe('event handlers', () => {
8-
let deleteStub: sinon.SinonStub;
8+
let accountDeleteStub: sinon.SinonStub;
9+
let saveDeleteStub: sinon.SinonStub;
910
let sentryStub: sinon.SinonStub;
11+
1012
beforeEach(() => {
1113
sinon.restore();
1214
sentryStub = sinon.stub(Sentry, 'captureException');
1315
});
16+
1417
afterAll(() => sinon.restore());
18+
1519
describe('with no handler errors', () => {
1620
beforeEach(() => {
17-
deleteStub = sinon.stub(handlers, Event.ACCOUNT_DELETION).resolves();
21+
accountDeleteStub = sinon
22+
.stub(handlers, Event.ACCOUNT_DELETION)
23+
.resolves();
24+
25+
saveDeleteStub = sinon.stub(handlers, Event.DELETE_ITEM).resolves();
1826
});
27+
1928
it('routes to the correct handler function based on detail-type', async () => {
20-
const records = {
29+
let records = {
2130
Records: [
2231
{
2332
body: JSON.stringify({
@@ -28,32 +37,55 @@ describe('event handlers', () => {
2837
},
2938
],
3039
};
40+
3141
await processor(records as SQSEvent);
32-
expect(deleteStub.callCount).toEqual(1);
33-
expect(deleteStub.getCall(0).args).toEqual([records.Records[0]]);
42+
expect(accountDeleteStub.callCount).toEqual(1);
43+
expect(accountDeleteStub.getCall(0).args).toEqual([records.Records[0]]);
44+
45+
records = {
46+
Records: [
47+
{
48+
body: JSON.stringify({
49+
Message: JSON.stringify({
50+
'detail-type': Event.DELETE_ITEM,
51+
}),
52+
}),
53+
},
54+
],
55+
};
56+
57+
await processor(records as SQSEvent);
58+
expect(saveDeleteStub.callCount).toEqual(1);
59+
expect(saveDeleteStub.getCall(0).args).toEqual([records.Records[0]]);
3460
});
35-
it('returns batchItemFailure and logs to Sentry if handler does not exist', async () => {
61+
it('is a NOOP if a handler does not exist', async () => {
3662
const records = {
3763
Records: [
3864
{
3965
body: JSON.stringify({
40-
Message: JSON.stringify({ 'detail-type': 'NOT_A_TYPE' }),
66+
Message: JSON.stringify({
67+
'detail-type': 'NOT_A_TYPE_I_CAN_HANDLE',
68+
}),
4169
}),
4270
messageId: 'abc',
4371
},
4472
],
4573
};
74+
4675
const res = await processor(records as SQSEvent);
47-
expect(res.batchItemFailures).toEqual([{ itemIdentifier: 'abc' }]);
48-
expect(sentryStub.callCount).toEqual(1);
49-
expect(sentryStub.getCall(0).args[0].message).toEqual(
50-
`Unable to retrieve handler for detail-type='NOT_A_TYPE'`
51-
);
76+
77+
// no failures/errors
78+
expect(res.batchItemFailures).toEqual([]);
79+
expect(sentryStub.callCount).toEqual(0);
80+
81+
// no handlers were called
82+
expect(accountDeleteStub.callCount).toEqual(0);
83+
expect(saveDeleteStub.callCount).toEqual(0);
5284
});
5385
});
5486
describe('with handler errors', () => {
5587
beforeEach(() => {
56-
deleteStub = sinon
88+
accountDeleteStub = sinon
5789
.stub(handlers, Event.ACCOUNT_DELETION)
5890
.rejects(Error('got an error'));
5991
});

lambda/events/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@ import { handlers } from './handlers';
1111
*/
1212
export async function processor(event: SQSEvent): Promise<SQSBatchResponse> {
1313
const batchFailures: SQSBatchItemFailure[] = [];
14+
1415
for await (const record of event.Records) {
1516
try {
1617
const message = JSON.parse(JSON.parse(record.body).Message);
17-
if (handlers[message['detail-type']] == null) {
18-
throw new Error(
19-
`Unable to retrieve handler for detail-type='${message['detail-type']}'`
20-
);
18+
const handler = handlers[message['detail-type']];
19+
20+
if (handler !== undefined) {
21+
await handler(record);
2122
}
22-
await handlers[message['detail-type']](record);
2323
} catch (error) {
2424
console.log(error);
2525
Sentry.captureException(error);
2626
batchFailures.push({ itemIdentifier: record.messageId });
2727
}
2828
}
29+
2930
return { batchItemFailures: batchFailures };
3031
}
3132

0 commit comments

Comments
 (0)