Skip to content

fix(parser): wrap envelope parse failures in ParseError - #5758

Merged
svozza merged 3 commits into
aws-powertools:mainfrom
Provokke:fix/parser-envelope-parse-error
Oct 10, 2026
Merged

svozza merged 3 commits into
aws-powertools:mainfrom
Provokke:fix/parser-envelope-parse-error

Conversation

@Provokke

@Provokke Provokke commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

SnsEnvelope, CloudWatchEnvelope, DynamoDBStreamEnvelope and KafkaEnvelope let a raw ZodError escape from parse(). invokeErrorHandler only handles ParseError, so the errorHandler option was skipped and the error was rethrown.

  • Wrap the envelope-level parse in the SNS, CloudWatch and DynamoDB Stream envelopes, and rethrow it as ParseError with the ZodError as cause. The messages match the ones each envelope's safeParse() already uses (Failed to parse SNS envelope, and so on).
  • In KafkaEnvelope.parse(), put extractEventSource() and the MSK/self-managed schema parse inside the same try, so an unknown eventSource becomes a ParseError too.
  • Wrap each Kafka record parse. The error message names the record's index and topic key (Failed to parse Kafka record at index 0 of mytopic-0), and the issue paths are prefixed with ['records', topicKey, index, 'value'] through prefixIssuePaths, the same way the other array envelopes do it. A transform that throws keeps its original error as cause, matching fix(parser): keep transform errors as the ParseError cause #5745.

I left KafkaEnvelope.safeParse() alone. It prefixes record issues with ['records', topicKey], without the record index or value. I can align it here or in a follow-up if you'd like.

Tests: the snippet from the issue passes (6/6). I added parse() tests for an invalid envelope in each of the four envelopes, for a Kafka record that fails the schema (message and path), for a throwing transform in Kafka, and for an unknown Kafka eventSource. There is also a middleware test that checks errorHandler is called when SnsEnvelope fails. Without the source change, all 8 new tests fail. The parser unit suite passes (306 tests) with the 100% coverage gate, and build:tests, test:unit:types and Biome on the changed files are clean.

Issue number: closes #5742


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.

SnsEnvelope, CloudWatchEnvelope, DynamoDBStreamEnvelope and KafkaEnvelope
let the envelope-level ZodError escape from parse(), so the errorHandler
option was skipped. KafkaEnvelope also threw raw errors for records that
failed the user schema and for an unknown eventSource.

Closes aws-powertools#5742
Comment thread packages/parser/src/envelopes/cloudwatch.ts Outdated
@svozza

svozza commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Do you need any help with this PR @Provokke. If you don't have capacity, I'm happy to complete it, you will still get credited in the git history.

@Provokke

Provokke commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

@svozza Will make fix now!

…arse-error

# Conflicts:
#	packages/parser/tests/unit/parser.middy.test.ts
Add parseOrThrow to envelope.ts and use it for the envelope and record parses in the array envelopes, instead of repeating the same try/catch in each one. Messages and issue paths are unchanged.
Copilot AI balanced review requested due to automatic review settings October 10, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Provokke

Copy link
Copy Markdown
Contributor Author

@svozza Done, ready for another look when you have a moment.

  • Added a parseOrThrow helper in envelope.ts and used it for both the envelope and record parses across the array envelopes (SQS, SNS, SNS-SQS, Kinesis, Firehose, CloudWatch, DynamoDB, Kafka). Error messages and issue paths are unchanged.
  • Merged main to clear the conflict.

The parser suite passes locally with 100% coverage, and build:tests and lint are clean. CI may need approval to run on the fork.

@svozza
svozza merged commit 77d5f9e into aws-powertools:main Oct 10, 2026
41 checks passed
@svozza

svozza commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Nice! LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PRs between 100-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: parser envelopes throw raw ZodError, bypassing errorHandler

3 participants