Reactjs 为什么无法捕获redux saga生成器中的错误?

Reactjs 为什么无法捕获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

这里是store/store.js

...

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
ting
PHOTO\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放在里面检查)。如果有人遇到这样的问题,我会把我的答案放在这里,这样他们就可以解决问题了。我不接受,因为这不是问题的直接答案。我把我的答案放在这里,如果有人面临这样的问题,那么他们可以解决。我不接受,因为这不是问题的直接答案。