formik icon indicating copy to clipboard operation
formik copied to clipboard

submitForm doesn't throw an error

Open b-ponomarenko opened this issue 5 years ago • 8 comments
trafficstars

🐛 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

b-ponomarenko avatar Jan 27 '20 12:01 b-ponomarenko

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?

ypresto avatar Feb 08 '20 18:02 ypresto

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

nnaku avatar Feb 12 '20 06:02 nnaku

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.

KevinAsher avatar Feb 21 '20 15:02 KevinAsher

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);
        }
      }}
    />
  )
}
`

KevinAsher avatar Mar 22 '20 23:03 KevinAsher

Another example https://codesandbox.io/s/elegant-star-0ru0y. In my case, I trigger form from the outside

archansel avatar Mar 29 '20 11:03 archansel

Any news on this? Came across the same issue.

artursvonda avatar Jun 29 '20 13:06 artursvonda

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

MrRainesE avatar Apr 25 '22 11:04 MrRainesE

facing the same issue

kareemmamdouhTO avatar Jun 25 '23 18:06 kareemmamdouhTO