Reactjs 为什么无法捕获redux saga生成器中的错误?
这里是store/store.jsReactjs 为什么无法捕获redux saga生成器中的错误?,reactjs,react-redux,axios,redux-saga,Reactjs,React Redux,Axios,Redux Saga,这里是store/store.js ... const initSagaMiddleware = createSagaMiddleware(); const middlewares = [initSagaMiddleware, fetchPhotosMiddleware, putPhotoMiddleware]; const middlewareEnhancer = applyMiddleware(...middlewares); ... initSagaMiddleware.run(r
...
const initSagaMiddleware = createSagaMiddleware();
const middlewares = [initSagaMiddleware, fetchPhotosMiddleware, putPhotoMiddleware];
const middlewareEnhancer = applyMiddleware(...middlewares);
...
initSagaMiddleware.run(rootSaga);
export default store;
sagas/api-saga.js
export default function* rootSaga() {
yield all([
photoWatcher()
]);
}
function* photoWatcher() {
yield takeEvery(PUT_PHOTO, putPhotoWorker);
}
function* putPhotoWorker(action) {
try {
const payload = yield call(putPhoto, action.urlParams, action.body);
yield put({ type: PHOTO_UPDATED, payload });
} catch (err) {
yield put({ type: API_ERROR_PUT_PHOTO, payload: err });
}
}
和服务/api.js
export function putPhoto(urlParams, body) {
return axios.put('/abc/' + urlParams.key + '/def/' + urlParams.id + '/edit', body)
.then(res => {
return res.data;
})
.catch(err => err);
}
我的问题是:即使当我从api调用中得到错误时,redux saga
put
tingPHOTO\u更新了,而不是api\u error\u put\u PHOTO
。我做错了什么?如何捕获错误?然后和捕获func中的承诺都会导致try
语句。正因为如此,我放弃了try…catch
,而是使用if…else
语句。像这样:
function* putPhotoWorker(action) {
const payload = yield call(putPhoto, action.urlParams, action.body);
if (isResponseTypeWhatIExpected) {
yield put({ type: PHOTO_UPDATED, payload });
} else {
yield put({ type: API_ERROR_PUT_PHOTO, payload });
}
}
then
和catch
在putPhoto
func中的承诺都导致了try
语句。正因为如此,我放弃了try…catch
,而是使用if…else
语句。像这样:
function* putPhotoWorker(action) {
const payload = yield call(putPhoto, action.urlParams, action.body);
if (isResponseTypeWhatIExpected) {
yield put({ type: PHOTO_UPDATED, payload });
} else {
yield put({ type: API_ERROR_PUT_PHOTO, payload });
}
}
问题是您试图捕捉相同的错误两次。只需从putPhoto
函数中删除catch,它就可以工作了
export function putPhoto(urlParams, body) {
return axios.put('/abc/' + urlParams.key + '/def/' + urlParams.id + '/edit', body)
.then(res => {
return res.data;
});
}
问题是您试图捕捉相同的错误两次。只需从putPhoto
函数中删除catch,它就可以工作了
export function putPhoto(urlParams, body) {
return axios.put('/abc/' + urlParams.key + '/def/' + urlParams.id + '/edit', body)
.then(res => {
return res.data;
});
}
问题是您正在捕获putPhoto
函数中的错误。只要拆下锁扣,它就会工作。可能吧,不。它不工作:/API是否抛出错误?当然。400错误请求。如果您在putPhoto函数中保留承诺catch
,则在发生错误时是否调用catch回调?(将console.log或smth放入其中进行检查)。问题是您正在捕获putPhoto
函数中的错误。只要拆下锁扣,它就会工作。可能吧,不。它不工作:/API是否抛出错误?当然。400错误请求。如果您在putPhoto函数中保留承诺catch
,则在发生错误时是否调用catch回调?(把console.log或smth放在里面检查)。如果有人遇到这样的问题,我会把我的答案放在这里,这样他们就可以解决问题了。我不接受,因为这不是问题的直接答案。我把我的答案放在这里,如果有人面临这样的问题,那么他们可以解决。我不接受,因为这不是问题的直接答案。