redux-form: throw new SubmissionError() causing Uncaught (in promise) error.

Hello! I am using Redux Thunk and React Form to do submission validations against my signup form. I want the errors returned by my API endpoint to appear inline in my form. Hence why I need SubmissionError. Everything works great without throwing a new submission error, the form does exactly what I need it to do. The problem is, when I put in the single line to throw new SubmissionError() I get the following error:

image

Here is my Action creator (using Redux Thunk):

import axios from 'axios';
import { browserHistory } from 'react-router';
import { CTAButtonUnload } from '../../buttons/CTAButton/CTAButtonUnload'
import { SubmissionError } from 'redux-form'

const ROOT_URL = 'XXXXXXXXXXXXXX';

export const signupUser = (formData) => {
    return function(dispatch) {
        axios.post(ROOT_URL + '/v1/user', {
                firstName: formData.firstName,
                lastName: formData.lastName,
                email: formData.email,
                password: formData.password
            })
            .then(response => {
                dispatch({
                    type: 'CTAButtonUnload',
                    payload: {
                        hover: false,
                        click: false,
                        loading: false
                    }
                })
                browserHistory.push('/signup/workspace');
            })
            .catch(response => {
                if(response.response.data.code == 403){
                    throw new SubmissionError({email: null, _error: response.response.data.description})
                }
                dispatch({
                    type: 'CTAButtonUnload',
                    payload: {
                        hover: false,
                        click: false,
                        loading: false
                    }
                })
            });
    }
}

Could this error be caused by a promise inside of SubmissionError()? I saw that others have had similar problems with this error, but they look to be using the Async validation option, I am not, so I am wondering if I am doing something wrong or if this is something with reduxForm?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 21
  • Comments: 17 (1 by maintainers)

Most upvoted comments

I encountered a similar problem and managed to fix this by adding a return on the submit handler.

from

handleSignup = (data) => {
        this.props.actions.signup(data).then((result) => {
            ...
        }).catch((error) => {
            if (error.data.status === 'XXXXXX') {
                throw new SubmissionError({
                    email: 'This email is already registered'
                })
            }
        })
    }

to

handleSignup = (data) => {
        return this.props.actions.signup(data).then((result) => {
            ...
        }).catch((error) => {
            if (error.data.status === 'XXXXXX') {
                throw new SubmissionError({
                    email: 'This email is already registered'
                })
            }
        })
    }

I’m calling handleSignup() from my redux form component:

<SignupForm onSubmit={this.handleSignup} />

I had some progress on this one. As it turned out, I was using redux-form/immutable all over my code, except when importing SubmissionForm, I did:

import { SubmissionForm } from 'redux-form';

But if you are using redux-form/immutable, you need to use:

import { SubmissionForm } from 'redux-form/immutable';

Apparently they are two different classes.

@JensenTStava In your mapDispatchToProps you should have this format:

const mapDispatchToProps = function(dispatch) {
  return {
    signupUser: (formData) => {
      return dispatch(signupUser(formData)); // Notice the return
    }
  };
};

Just updated to webpack 2, and started encountering this bug.

In my case, the issue is not from redux-form but webpack 2 and / or babel.

My code breaks if I imported SubmissionError like this:

import { SubmissionError } from 'redux-form';

// somewhere in promise chain:
throw  new SubmissionError({errors});

But works if I import it like this:

import SubmissionError from 'redux-form/lib/SubmissionError'

I was able to figure out this line does error instanceof SubmissionError and returning false.

It lead me to believe somehow webpack 2 is generated two copies SubmissionErrors…

Update:

Created a simple demonstration for this:

https://github.com/aq1018/webpack2-redux-form-submission-error-weirdness

alt text

I was stuck for one day with similar Error. I was returning all promises in Project and I still couldn’t figure it out. Then I ran into this https://stackoverflow.com/questions/38481857/getting-cannot-call-a-class-as-a-function-in-my-react-project

I was using react router 4 like this: <Route exact path="/events/checkout/:id/" render={(props) => <CheckoutContainer {...props} />} />} />

then I change it into this: <Route exact path="/events/checkout/:id/" component={(props) => <CheckoutContainer {...props} />} />} />

So, I just changed render to component and It is working! Problem is that render most likely treat all components as functional.

For me this issue was caused by not adding a return declaration before my ajax call. So instead of axios.post(URL, payload) I had to make sure return axios.post(URL, payload) was used. The throw new SubmissionError(…) call was in the .catch( error => … block of the axios call.

@JensenTStava I was under the impression that ‘mapsDispatchToProps’ was only for version 5 and older.

I would love to see a solution to this issue because I am having a similar problem as the OP.

@francosioquim your solution was it. The handleSubmit function that redux-form defines probably look (conceptually) something like this:

function handleSubmit(event) {
   const formValues = getFormValues(event);
   userDefinedSubmit(formValues).catch(
      submissionError => putErrorsInFormState(submissionError)
   )
}

I’m abstracting away a lot, but the idea is that handleSubmit function will only work if the userDefinedSubmit function returns a thenable. Thank you!