Skip to content

fix(parser): let SnsSqsEnvelope safeParse propagate schema errors - #5846

Merged
dreamorosi merged 1 commit into
aws-powertools:mainfrom
vishwakt:fix/5829-parser-sns-sqs-safeparse-propagate
Oct 26, 2026
Merged

dreamorosi merged 1 commit into
aws-powertools:mainfrom
vishwakt:fix/5829-parser-sns-sqs-safeparse-propagate

Conversation

@vishwakt

Copy link
Copy Markdown
Contributor

Summary

SnsSqsEnvelope.safeParse caught errors thrown by the user's schema and turned them into a custom Zod issue holding only the message. A bug in the schema was reported as invalid input, and the original error was lost. Every other envelope's safeParse lets the error propagate, in line with Zod's guidance that transforms should never throw. This makes SnsSqsEnvelope consistent with them.

Changes

  • Removed the try/catch around SnsSqsNotificationSchema.safeParse and the user's schema.safeParse in SnsSqsEnvelope.safeParse (packages/parser/src/envelopes/sns-sqs.ts), so an error thrown by the schema propagates unchanged.
  • Kept the try/catch around JSON.parse(record.body), so a malformed record body still returns { success: false } with a custom issue at Records[i].body. The existing tests for that case are unchanged and pass.
  • Replaced the test that asserted the message-only custom issue with one that asserts safeParse throws the original error. It fails against main.

SnsSqsEnvelope.parse is unchanged, since it already wraps the thrown error in a ParseError with the original error as cause.

Issue number: closes #5829


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

SnsSqsEnvelope.safeParse caught errors thrown by the user's schema and
turned them into a custom Zod issue holding only the message, so a bug
in the schema was reported as invalid input and the original error was
lost. Every other envelope lets the error propagate, in line with Zod's
guidance that transforms should never throw.

Remove the try/catch around the SNS notification and user schema
parsing, and keep the one around JSON.parse so a malformed record body
still returns a failed result with a custom issue.

Closes aws-powertools#5829
@dreamorosi
dreamorosi enabled auto-merge (squash) October 26, 2026 08:33
@dreamorosi
dreamorosi merged commit db8ac34 into aws-powertools:main Oct 26, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M PR between 30-99 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SnsSqsEnvelope safeParse swallows errors thrown by a schema transform

2 participants