Skip to content

fix(parser): fix cause errors nested structure #3250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 25, 2024

Conversation

am29d
Copy link
Contributor

@am29d am29d commented Oct 23, 2024

Summary

This PR fixes the bug of nested errors inside the ParsedResultError structure.

Changes

In the generic Envelope we have returned a ParsedResultError structure

{
	success: false,
	error: new ParseError('foo'),
	originalEvent: data
}

In all cases the Envelope was called from specific envelopes such as ApiGateway, EventBridge etc. and in all of such cases we repackage the entire response again, which causes error.cause to be ParseError (from the Envelope call), and the not the expected ZodError.

Therefore, the Envelope.safeParse now returns a generic error or whatever we catch within the safeParse and no originalEvent. The reason for that is that the specific envelope has the entire original payload and should have the responsibility to return this information back to the user. At that point the originalEvent is not relevant for the Envelope object at all.

I had to modify the return because TypeScript is not aware of the if-statement before the return:

if (!parsedEnvelope.success) {
      return {

      };
    }
	
    return parsedBody as ParsedResultSuccess<z.infer<T>>;

I have also added more specific error assertions in the tests.

Please provide a summary of what's being changed

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #3208


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.

@am29d am29d requested a review from a team October 23, 2024 19:18
@am29d am29d requested a review from a team as a code owner October 23, 2024 19:18
@boring-cyborg boring-cyborg bot added parser This item relates to the Parser Utility tests PRs that add or change tests labels Oct 23, 2024
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Oct 23, 2024
@am29d am29d self-assigned this Oct 23, 2024
@github-actions github-actions bot added the bug Something isn't working label Oct 23, 2024
dreamorosi
dreamorosi previously approved these changes Oct 25, 2024
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Thank you for fixing the issue and for the PR, Alex.

I have left a minor comment in one of the envelope files, but it's not a blocking one. Feel free to merge as-is if you prefer to leave it.

Copy link

@am29d am29d requested a review from dreamorosi October 25, 2024 17:52
@dreamorosi dreamorosi merged commit 1ff97cb into main Oct 25, 2024
21 checks passed
@dreamorosi dreamorosi deleted the 3208/ParsedResultError-nested branch October 25, 2024 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser This item relates to the Parser Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: ParsedResultError contains nested ParseErrors instead of a ParseError with a cause of ZodError
2 participants