Javascript 如何在axios catch()块中重新抛出捕获的错误
在Javascript 如何在axios catch()块中重新抛出捕获的错误,javascript,reactjs,axios,redux-saga,Javascript,Reactjs,Axios,Redux Saga,在axios中,为什么在catch()中不允许抛出新错误()? 我正处于这样的需求中,如果服务器返回错误,catch块应该抛出一个错误,稍后将由reduxsaga处理,并将调度适当的操作 API调用: export const userSignupApi = userDetails => { const endpoint = `${URL_ROOT}${URN_SIGNUP}`; axios .post(endpoint, userDetails) .then
axios
中,为什么在catch()中不允许抛出新错误()?
我正处于这样的需求中,如果服务器返回错误,catch块应该抛出一个错误,稍后将由reduxsaga处理,并将调度适当的操作
API调用:
export const userSignupApi = userDetails => {
const endpoint = `${URL_ROOT}${URN_SIGNUP}`;
axios
.post(endpoint, userDetails)
.then(response => {
console.log("Response: ", response);
//do something with the response
})
.catch(error => {
throw new Error(error.response.data.message);
});
};
由于上面的catch块,我得到了未处理的拒绝(错误)
。
以下是我处理这项业务的传奇故事:
import { call, takeLatest, put } from "redux-saga/effects";
function* handleUserSignup(action) {
try {
yield call(userSignupApi, action.userDetails);
yield put(userSignupSuccess()); //dispatching success action
} catch (error) {
yield put(userSignupFail(error)); //dispatching error action
}
}
function* watchUserSignup() {
yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}
编辑:为什么我想要上面的代码结构?因为这样可以很容易地对API代码和saga代码进行单元测试。您使用的是try catch
,因为您不想在浏览器控制台中抛出要在catch
中处理的错误。在catch
中抛出错误将删除其用途。如果要抛出error remove请尝试catch
(这不是推荐的方法)
更新
For axios catch方法捕获api url引发的任何错误。如果您不想捕捉错误并在浏览器中显示它,可以删除捕捉块,也可以调用处理错误的操作。有关promise catch的更多信息,您可以参考在userSignupAPI
中创建的promise没有在任何地方使用(甚至没有返回),因此当在catch
中抛出错误时,promise链将解析为(未捕获的)拒绝的promise,从而导致错误
在userSignupAPI
的调用者中,您应该等待
调用,以便中的try
/catch
将看到抛出的错误:
export const userSignupAPI = userDetails => {
const endpoint = `${URL_ROOT}${URN_SIGNUP}`;
return axios
.post(endpoint, userDetails)
.then(response => {
console.log("Response: ", response);
})
.catch(error => {
throw new Error(error.response.data.message);
});
};
async function* handleUserSignup(action) {
try {
yield await call(userSignupApi, action.userDetails);
yield put(userSignupSuccess()); //dispatching success action
} catch (error) {
yield put(userSignupFail(error)); //dispatching error action
}
}
function* watchUserSignup() {
yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}
(确保call
返回由userSignupApi
返回的Promise
)我得到了这个工作。我完全错了。正如@certianPerformance所建议的,在阅读了一些问题和github问题之后,我找到了正确的方法来处理这个问题。我不应该返回api响应,而是应该返回承诺
解决方案如下:
export const userSignupApi = userDetails => {
const endpoint = `${URL_ROOT}${URN_SIGNUP}`;
return axios.post(endpoint, userDetails);
};
传奇:
把问题再读一遍。它不是关于try-catch
,而是关于axios中的promise
功能(userSignupApi,action.userDetails)
甚至没有向服务器发出请求(userSignupApi
)。您的调用功能是什么?问题代码中没有定义对不起,我没有提到。它由redux saga/effects
import { call, takeLatest, put } from "redux-saga/effects";
function* handleUserSignup(action) {
try {
const response = yield call(userSignupApi, action.userDetails);
response.then(response => {
const location = response.headers.location;
put(userSignupSuccess(location));
put(newUserWelcomeNote("Welcome user"));
});
} catch (error) {
const errorMessage = error.response.data.message;
yield put(userSignupFail(errorMessage));
}
}
function* watchUserSignup() {
yield takeLatest(NEW_USER.SIGNUP, handleUserSignup);
}