Skip to content

Use safeParse instead of parse for zod schemas to improve error reporting #689

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

Open
jds2501 opened this issue Apr 20, 2025 · 4 comments
Open
Labels
good first issue Good for newcomers

Comments

@jds2501
Copy link

jds2501 commented Apr 20, 2025

The error reporting on zod parsing failures right now reports "Invalid response schema," which made debugging some stagehand test failures difficult. We can improve error handling by using a pattern like this:

const result = schema.safeParse(data);

if (!result.success) {
// log or rethrow with detail
throw new MyCustomError(result.error.format());
}

const parsed = result.data;

This would allow you to get more explicit guidance on why Zod schema parsing failed - example:

{
name: { _errors: ['Required'] },
age: { _errors: ['Expected number, received string'] },
_errors: []
}

@kamath kamath added the good first issue Good for newcomers label Apr 20, 2025
@kamath
Copy link
Member

kamath commented Apr 20, 2025

Thanks for the feedback!! We can certainly take this on, unless someone else wants to handle this :)

@CtrlAltGiri
Copy link

I'm happy to take this!

@CtrlAltGiri
Copy link

Have a first iteration here: #690

@CtrlAltGiri
Copy link

@kamath can you review this please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants