Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这个错误没有在我的Redux操作中结束';什么事?_Javascript_Reactjs_Promise_Async Await - Fatal编程技术网

Javascript 为什么这个错误没有在我的Redux操作中结束';什么事?

Javascript 为什么这个错误没有在我的Redux操作中结束';什么事?,javascript,reactjs,promise,async-await,Javascript,Reactjs,Promise,Async Await,在这个应用程序中,我使用AsyncWait尝试通过几个函数处理API调用。在测试sad路径错误路由时,我注意到错误不会在捕获中结束 启动整个过程的行动 export const submitBasicDetails = form => async dispatch => { try { const response = await interstitialHandlerPromise(Actions.SUBMIT_FORM, form); // <- w

在这个应用程序中,我使用AsyncWait尝试通过几个函数处理API调用。在测试sad路径错误路由时,我注意到错误不会在
捕获中结束

启动整个过程的行动

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.'
        });
    }
};
最后,
postForm
函数请求上述函数内部的

// /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);
    }
};
///测试将导致500错误,因为它不存在
export const postForm=async(form,END_POINT='/api/test')=>{
const resPayload=form.data;
const resul=`${BASE\u URL}${V1}${END\u POINT}`;
试一试{
const res=等待axios.post(resul,{…resPayload});
返回{
物件
};
}捕获(e){

console.log('postforme',e);//明白了!我需要抛出错误,而不是返回错误

在postForm函数中:

} catch (e) {
    console.log('postInitialLoss e', e);
    throw new Error(e.message);
}
然后在
初始化间和重新编程中

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
    }
};

明白了!我需要抛出错误,而不是返回错误

在postForm函数中:

} catch (e) {
    console.log('postInitialLoss e', e);
    throw new Error(e.message);
}
然后在
初始化间和重新编程中

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
    }
};

postForm
是一个承诺并且正在捕获错误,因此它只返回捕获返回的任何内容
postForm
返回的
是一个错误,而不是
throw
interstitutialhandlerPromise
postForm
是一个承诺并且正在捕获错误,因此它只返回wha无论捕获返回
postForm
是否返回
错误,而不是
抛出
ing它使
中间句柄承诺无效
!如果您所做的只是重新
抛出
错误,您只需删除
try
捕获
块,让异常正常传播。@Bergi真的!如果您所做的只是重新抛出错误,那么您只需要删除
try
/
catch
块,让异常正常传播。@Bergi这是真的!