Skip to content

Commit

Permalink
fix: improve sqs event adapter error handling (#541)
Browse files Browse the repository at this point in the history
This is to prevent ops alerts when no messages are in the payload.
  • Loading branch information
solaris007 authored Jan 15, 2025
1 parent f30ec0b commit 2e9fca8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
40 changes: 24 additions & 16 deletions packages/spacecat-shared-utils/src/sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@

import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs';
import AWSXray from 'aws-xray-sdk';
import { hasText } from './functions.js';

import { hasText, isNonEmptyArray } from './functions.js';

function badRequest(message) {
return new Response('', {
status: 400,
headers: { 'x-error': message },
});
}

/**
* @class SQS utility to send messages to SQS
Expand Down Expand Up @@ -94,24 +102,24 @@ export function sqsEventAdapter(fn) {
const { log } = context;
let message;

// currently not processing batch messages
const records = context.invocation?.event?.Records;

if (!isNonEmptyArray(records)) {
log.warn('Function was not invoked properly, event does not contain any records');
return badRequest('Event does not contain any records');
}

const record = records[0];

log.info(`Received ${records.length} records. ID of the first message in the batch: ${record.messageId}`);

try {
// currently not publishing batch messages
const records = context.invocation?.event?.Records;
if (!Array.isArray(records) || records.length === 0) {
throw new Error('No records found');
}

log.info(`Received ${records.length} records. ID of the first message in the batch: ${records[0]?.messageId}`);
message = JSON.parse(records[0]?.body);
log.info(`Received message with id: ${records[0]?.messageId}`);
message = JSON.parse(record.body);
log.info(`Received message with id: ${record.messageId}`);
} catch (e) {
log.warn('Function was not invoked properly, message body is not a valid JSON', e);
return new Response('', {
status: 400,
headers: {
'x-error': 'Event does not contain a valid message body',
},
});
return badRequest('Event does not contain a valid message body');
}
return fn(message, context);
};
Expand Down
22 changes: 22 additions & 0 deletions packages/spacecat-shared-utils/test/sqs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ describe('SQS', () => {
const handler = sqsEventAdapter(exampleHandler);
const response = await handler(emptyRequest, contextNoRecords);

expect(response.status).to.equal(400);
expect(response.headers.get('x-error')).to.equal('Event does not contain any records');
});

it('returns bad request when record is not valid JSON', async () => {
const ctx = {
log: console,
invocation: {
event: {
Records: [
{
body: 'not a valid JSON',
messageId: 'abcd',
},
],
},
},
};

const handler = sqsEventAdapter(exampleHandler);
const response = await handler(emptyRequest, ctx);

expect(response.status).to.equal(400);
expect(response.headers.get('x-error')).to.equal('Event does not contain a valid message body');
});
Expand Down

0 comments on commit 2e9fca8

Please sign in to comment.