Javascript 为什么这个错误没有在我的Redux操作中结束';什么事?
在这个应用程序中,我使用AsyncWait尝试通过几个函数处理API调用。在测试sad路径错误路由时,我注意到错误不会在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
捕获中结束
启动整个过程的行动
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这是真的!