powertools-lambda-typescript icon indicating copy to clipboard operation
powertools-lambda-typescript copied to clipboard

Feature request: add custom Error for envelopes

Open am29d opened this issue 2 years ago • 1 comments

Use case

When parsing custom schema in the envelope we currently propagate any error from zod back to the user. When there is an error, the mental process is first to localise it between envelope or custom schema. We should provide a custom Error that clearly points out which part of the structure is invalid. This will allow to faster resolution of the error.

Solution/User Experience

No solution yet.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

am29d avatar Dec 19 '23 13:12 am29d

Revisiting this after a few months since the last interaction.

So far, most likely due to the utility being in beta, there hasn't been much demand for this.

We'll check back in a few months to see if things have changed, in the meanwhile Zod might have launched its next major version which is expected to include changes to the errors API.

dreamorosi avatar Jul 30 '24 13:07 dreamorosi

As part of a bug bash and refactoring of envelopes we have been making it so errors thrown by any of the envelopes are ParserError instances that contain the ZodError as cause. Both errors are customized to their respective envelopes and include contextual info like the index of Record in a SQS queue where appropriate. Below an example:

new ParseError('Failed to parse API Gateway body', {
  cause: new ZodError([
    {
      code: 'invalid_type',
      expected: 'string',
      received: 'undefined',
      path: ['path'],
      message: 'Required',
    },
    {
      code: 'invalid_type',
      expected: 'object',
      received: 'null',
      path: ['body'],
      message: 'Expected object, received null',
    },
  ]),
})

With this in mind, I'm closing the issue as completed since we have improved on the previous DX.

If anyone reads this issue in the future and would like to propose further improvements or changes to how we enhance Zod errors, please feel free to open a discussion.

dreamorosi avatar Jan 23 '25 20:01 dreamorosi

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Jan 23 '25 20:01 github-actions[bot]