Skip to content
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
9 changes: 7 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ export async function handler(event, context) {
return await handleDefault(event, context);
} catch (err) {
console.error('Error processing event', err);
// For HTTP or WebSocket requests, return an error response
if (event.httpMethod || (event.version === '2.0' && (event.requestContext?.http?.method || event.requestContext?.routeKey))) {
const isObjectEvent = typeof event === 'object' && event;
const isHttpOrWs =
isObjectEvent &&
(event.httpMethod ||
(event.version === '2.0' &&
(event.requestContext?.http?.method || event.requestContext?.routeKey)));
if (!isObjectEvent || isHttpOrWs) {
return {
statusCode: 500,
headers: { 'Content-Type': 'application/json' },
Expand Down
10 changes: 10 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,14 @@ describe('handler', () => {
const result = await handler(event, context);
expect(result).toEqual({ processed: 1 });
});

test('gracefully handles undefined event', async () => {
const context = { awsRequestId: '1' };
const result = await handler(undefined, context);
expect(result).toEqual({
statusCode: 500,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ error: 'Internal Server Error' }),
});
});
});
Loading