为什么此错误未在Redux Action的Catch中结束?

里昂·加班

在此应用程序中,我使用async等待来尝试通过几个函数来处理API调用。在测试悲伤的路径错误路由时,我注意到该错误并未最终出现在中catch

开始整个过程​​的动作

export const submitBasicDetails = form => async dispatch => {
    try {
        const response = await interstitialHandlerPromise(Actions.SUBMIT_FORM, form); // <- we end up here
        console.log('submitBasicDetails response', response); // <-- we see the error here
        const {id} = response.data;

        dispatch({
            type: Actions.SUBMIT_FORM,
            id
        });

        return response;
    } catch (error) {
        console.log('ACTION', error); // <-- instead of here :()
        dispatch({
            type: Actions.SUBMIT_FORM_FAIL,
            id: null,
            error: 'There was a server error, please try again later.'
        });
    }
};

然后,表单数据将命中该interstitialHandlerPromise位置,我们在决定要使用哪种API方法:

export const interstitialHandlerPromise = (type, data) => {
    const {requestMethod, config} = determineRequestMethod(type);

    const requests = requestMethod(data);

    return new Promise((resolve, reject) =>
        interstitialHandler(requests, config)
            .then(response => resolve(response))
            .catch(error => {
                console.log('intersitial error', error);
                reject(error);
            })
    );
};

最后postFormrequests上面函数内部的函数:

// /test will cause a 500 error since it doesn't exist
export const postForm = async (form, END_POINT = '/api/test') => {
    const resPayload = form.data;
    const resUrl = `${BASE_URL}${V1}${END_POINT}`;

    try {
        const res = await axios.post(resUrl, {...resPayload});

        return {
            res
        };
    } catch (e) {
        console.log('postForm e', e); // <-- This hits because `/test` returns 500
        return new Error(e.message);
    }
};
里昂·加班

得到它了!除了返回错误,我还需要抛出错误。

在postForm函数中:

} catch (e) {
    console.log('postInitialLoss e', e);
    throw new Error(e.message);
}

然后在intersitialHandlerPromise

export const interstitialHandlerPromise = async (type, data) => {
    const {requestMethod, config} = determineRequestMethod(type);

    const requests = requestMethod(data);

    try {
        const response = await interstitialHandler(requests, config);
        return response;
    } catch(e) {
        throw new Error(e); // <- this gets hit
    }
};

现在,我们终于回到动作中catch

} catch (error) {
    console.log('ACTION (SHOULD GET HERE)!', error); // <-- WE are here! :D
    dispatch({
        type: Actions.SUBMIT_BASIC_DETAILS_FAIL,
        initialLossCreated: false,
        error: 'There was a server error, please try again later.'
    });
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么此代码未在“错误”事件中捕获错误?

为什么未在Google Maps(JavaScript)中显示此SVG?

为什么此Javascript代码未在表中添加行

为什么此语句引发“意外结束”错误?

为什么此淘汰赛数组未在DOM中更新?

为什么此ImageMagick转换调用未在我的图像中添加居中文本?

在JSFiddle中,为什么未在`window`上定义此全局变量?

如果未在数据中声明此值,为什么会有反应性?

为什么此函数返回未在Java中返回预期结果?

为什么此链表的代码在HackerRank中显示错误?

为什么此程序中没有显示段错误?

为什么在Glassfish 4.1中此证书错误?

为什么此代码中存在分段错误?

为什么在pycharm中安装pygame时出现此错误

为什么此数据框中的索引错误

为什么catch块给出了一个错误,并且变量未在Java中初始化

为什么在IDLE中运行此脚本时为什么没有收到错误消息?

为什么此代码会产生错误“结束标记“ </ c:when”不平衡”?

为什么RxJS finalize()箭头函数代码似乎未在将代码上传到Firebase Storage的此代码中执行?

如果未在PHP中声明类,为什么不抛出“ new”错误?

为什么我的Axios实例未在捕获到的错误中返回响应?

Arm 模板 - 为什么我收到错误消息,提示资源未在模板中定义?

当我尝试在 Button(action: {}) 中键入 Text() 或 Image() 时,为什么会出现此错误?

为什么表未在phpmyadmin中创建

为什么未在Express中设置cookie?

为什么表未在mysql中更新

为什么此功能不起作用,但是在Eclipse中却不显示错误?

我的C ++代码在宏+模板编译中失败,为什么会出现此错误?

在Java中为什么会出现此错误:“属性值必须为常数”?