Closed
Description
Expected Behavior
When working with the SqsEnvelope
I should be able to extract the message body regardless of whether it's a plain text string or a JSON-stringified one.
Current Behavior
When the message body is not a JSON-stringified object the envelope throws a runtime error as it's unable to parse it with JSON.parse()
.
Code snippet
import { z } from 'zod';
import { SqsEnvelope } from '@aws-lambda-powertools/parser/envelopes';
export const handler = async (event) => {
try {
SqsEnvelope.parse(event, z.string());
} catch (error) {
console.error(error);
}
}
Steps to Reproduce
Send a message like this:
aws sqs send-message \
--queue-url https://sqs.eu-west-1.amazonaws.com/123456789012/sqs-parser-MyQueue \
--message-body 'hello'
Observe the logs:
ParseError: Failed to parse envelope. This error was caused by:
Unexpected token 'h', "hello" is not valid JSON.
Possible Solution
We should avoid parsing by default and instead leave it to the caller to decide.
While working on this, we should also check the SQS Schema and the SNS + SQS Envelope, since they share some of the code.
Additionally, we should ensure the schema fails parsing when the Records
array is empty, as mentioned in #3475.
Powertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
22.x
Packaging format used
npm