Feature request: add custom Error for envelopes
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
- [X] This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- [ ] Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
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.
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.
⚠️ 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.