Skip to content
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

Caught errors are not distinguishable from uncaught errors #1839

Closed
lewischa opened this issue Jan 9, 2023 · 4 comments · Fixed by #1926
Closed

Caught errors are not distinguishable from uncaught errors #1839

lewischa opened this issue Jan 9, 2023 · 4 comments · Fixed by #1926
Labels
bug-confirmed Bug report that is confirmed

Comments

@lewischa
Copy link

lewischa commented Jan 9, 2023

In v3.20.x errors handled via the catch operator are logged along with errors that actually cause the parsing to fail. This makes it difficult to differentiate "fatal", uncaught errors from non-fatal, caught/handled errors. This isn't a huge deal in smaller schemas, but becomes very tedious with larger ones.

Is there a way to suppress caught errors from parse and safeParse, or perhaps mark them as caught?

Example

See the interactive example on Stackblitz, but here's the code for a minimal reproduction:

const schema = z.object({
  stringProp: z.string(),
  numberPropWithCatch: z.number().catch(0),
});

schema.parse({
  // This value is of incorrect type and causes the parsing to fail
  stringProp: {},
  // This value is of incorrect type as well, but DOESN'T
  // cause parsing to fail as it's caught in the schema.
  // However, the error is reported in the overall list
  // of parsing errors.
  numberPropWithCatch: '',
});

Running this code results in an error as expected, but notice how both the fatal and caught errors are displayed:
image

@JacobWeisenburger JacobWeisenburger added the bug-confirmed Bug report that is confirmed label Jan 10, 2023
@JacobWeisenburger
Copy link
Collaborator

This seems like a bug to me. I don't think numberPropWithCatch should show up in the errors.

const schema = z.object( {
    stringProp: z.string(),
    numberPropWithCatch: z.number().catch( 0 ),
} )

const result = schema.safeParse( {
    stringProp: {},
    numberPropWithCatch: '',
} )

!result.success && console.log( result.error.flatten() )
// {
//     formErrors: [],
//     fieldErrors: {
//       stringProp: [ "Expected string, received object" ],
//       numberPropWithCatch: [ "Expected number, received string" ]
//     }
// }

@colinhacks
Copy link
Owner

Agreed. Good catch!

@maxArturo
Copy link
Contributor

@lewischa @JacobWeisenburger took a crack at this on #1926. Let me know what you think!

colinhacks pushed a commit that referenced this issue Jan 29, 2023
* fix: [#1839] remove caught errors from issues

* Generalize implementation

---------

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
@lewischa
Copy link
Author

lewischa commented Feb 2, 2023

Thank you all! Is there a "dev" or "beta" release that includes that fix, or would you recommend forking/cloning?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-confirmed Bug report that is confirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants