formik
formik copied to clipboard
submitForm doesn't throw an error
🐛 Bug report
Current Behavior
The promise don't rejected if form is invalid.
Expected behavior
The promise will be rejected if form is invalid.
Reproducible example
https://codesandbox.io/s/amazing-bas-y5jq4
Suggested solution(s)
Additional context
Your environment
| Software | Version(s) |
|---|---|
| Formik | 2.1.2 |
| React | 16.12.0 |
| Browser | Chrome 79.0.3945.117 |
It is catched here. https://github.com/jaredpalmer/formik/blob/f117c04738ed218b5eb8916d7189e0849962d50d/packages/formik/src/Formik.tsx#L841
I want to handle promise rejection error to send it sentry or etc.
Formik.errorHandler = () => ... or something else?
as #2104 says submitForm will throw error only if error is instance of Error 😩
Im not sure but, what if we throw Error and object literal here https://github.com/nnaku/formik/blob/3fec628d31ec27ff8f168b9562f874fc640e4a7a/packages/formik/src/Formik.tsx#L801-L805
and test reason (instance of Error) here. https://github.com/nnaku/formik/blob/3fec628d31ec27ff8f168b9562f874fc640e4a7a/packages/formik/src/Formik.tsx#L841-L848
This will pass current tests, but I'm not sure is it acceptable workaround. @jaredpalmer
Yeah, this creates an issue for the use case of a global error handler. Since unhandled promise rejections won't bubble up anymore, we will have to manually do the unhandled error logic (send to error tracking service, show unexpected error UI to the user and etc) on all of our forms.
As a workaround, one can make a simple wrapper around Formik, for example
export default ({onSubmit, ...rest}) => {
return (
<Formik
{...rest}
onSubmit={async (...submitProps) => {
try {
await onSubmit(...submitProps);
} catch (err) {
// report error to whatever service...
// rethrow error for compatibility
throw err;
} finally {
submitProps[1].setSubmitting(false);
}
}}
/>
)
}
`
Another example https://codesandbox.io/s/elegant-star-0ru0y. In my case, I trigger form from the outside
Any news on this? Came across the same issue.
Another example https://codesandbox.io/s/elegant-star-0ru0y. In my case, I trigger form from the outside
any news on this? facing the same issue
facing the same issue