如何使用redux-thunk的redux-form捕获并抛出错误?

时间:2019-06-20 08:15:25

标签: redux redux-form redux-thunk

我正在尝试处理应用程序中的发布错误。我正在使用redux-thunk进行异步操作。在成功方法中,它工作得很好,但是我无法设法捕获错误并向表单抛出错误。实际上,它调度我自己的动作,即loginFailed动作。

formOnSubmitHandler = event => {
        // some codes here...
        const { values } = this.props.form;

        const { from } = this.props.location.state || { from: { pathname: HOME } };
        this.props.userLogin(values, from);

        // some codes here...
    };

这是我的用户登录redux-thunk操作:

export const userLogin = user => {
    return dispatch => {
        dispatch(loginRequested());
        return identityApi
            .post(AUTH_SIGN_IN, user)
            .then(response => {
                if (response.data.isSuccess) {
                    const {
                        accessToken,
                        refreshToken,
                        expires,
                        email,
                        name,
                        surname,
                        id,
                        phoneNumber,
                        isInformed
                    } = response.data.data;

                    dispatch(
                        loginSucceed({
                            currentUser: {
                                id,
                                name,
                                surname,
                                email,
                                phoneNumber,
                                isInformed
                            }
                        })
                    );
                    dispatch(
                        setToken({
                            accessToken,
                            refreshToken,
                            expires
                        })
                    );
                }
            })
            .catch(err => {
                dispatch(loginFailed(err.response.data.error.message)); // it is working
                throw new SubmissionError({
                    _error: err.response.data.error.message
                });
            });
    };
};

0 个答案:

没有答案