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

Internal submitForm implementation does not throw errors #1636

Closed
andrewMuntyan opened this issue Jun 24, 2019 · 5 comments · Fixed by #1637
Closed

Internal submitForm implementation does not throw errors #1636

andrewMuntyan opened this issue Jun 24, 2019 · 5 comments · Fixed by #1637

Comments

@andrewMuntyan
Copy link
Contributor

🚀 Feature request

Catch errors thrown from onSubmit inside of form/fields components

Current Behavior

Formik Internal implementation of submitForm does not throw any errors.
It is not possible to get in catch hook with the following example:

const onSaveButtonClick = async () => {
        // submitForm - formik internal method. It invokes `onSubmit` method
        //which is passed with props to formik
        const { submitForm } = props.formikActions;
        return submitForm()
          .then(promise => {
           // i'm always getting here
          })
          .catch((e: any) => {
           //cant get here
          });
      };

Here is a sandbox exaple

Desired Behavior

Have the ability to catch errors which are thrown from onSubmit method passed to formik as a prop

Suggested Solution

const submitForm = useEventCallback(() => {
    // ...some code here
    return validateFormWithHighPriority().then(
        // ...some code here
          return Promise.resolve(executeSubmit())
            .then(() => {
              if (!!isMounted.current) {
                dispatch({ type: 'SUBMIT_SUCCESS' });
              }
            })
            .catch(_errors => {
              if (!!isMounted.current) {
                dispatch({ type: 'SUBMIT_FAILURE' });
              }
            // THROW ERROR HERE
            });
        // ...some code here
        return;
      }
    );
  }, [executeSubmit, validateFormWithHighPriority]);

Who does this impact? Who is this for?

This is a cool additional feature for all users. In this way, internal submitForm method (which could be used inside component ) will completely implement Promise interface.

Describe alternatives you've considered

Additional context

@slorber
Copy link
Collaborator

slorber commented Jun 25, 2019

would also expect to work this way and be able to catch submit failures with formik.submitForm().catch()

@jaredpalmer
Copy link
Owner

Can u submit a pr?

@slorber
Copy link
Collaborator

slorber commented Jun 26, 2019

@andrewMuntyan you want to contribute or I'll do it? (more likely next week)

@andrewMuntyan
Copy link
Contributor Author

@slorber I'll try to make it faster. Check this issue next week. If there is no PR, pls make it happen :)

andrewMuntyan pushed a commit to andrewMuntyan/formik that referenced this issue Jun 26, 2019
… chain if handleSubmit has returned rejected Promise
andrewMuntyan pushed a commit to andrewMuntyan/formik that referenced this issue Jun 26, 2019
… chain if handleSubmit has returned rejected Promise. logs removed
andrewMuntyan pushed a commit to andrewMuntyan/formik that referenced this issue Jun 26, 2019
… chain if handleSubmit has returned rejected Promise. typo
@andrewMuntyan
Copy link
Contributor Author

i've made related PR - #1637

Also, my CodeSandBox example was not correct, so I've fixed it.

andrewMuntyan pushed a commit to andrewMuntyan/formik that referenced this issue Jun 27, 2019
… chain if handleSubmit has returned rejected Promise. Throw originally throwed error
jaredpalmer pushed a commit that referenced this issue Jun 27, 2019
* feature #1636 - submitForm helper should not brake promise chain if handleSubmit has returned rejected Promise

* feature #1636 - submitForm helper should not brake promise chain if handleSubmit has returned rejected Promise. logs removed

* feature #1636 - submitForm helper should not break promise chain if handleSubmit has returned rejected Promise. typo

* feature #1636 - submitForm helper should not break promise chain if handleSubmit has returned rejected Promise. Throw originally throwed error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants