Redux Saga 在出错后停止
Redux Saga stops after error
我正在组合来自不同模块的一组 Sagas(一些 takeEvery
和 takeLatest
),并使用 yield all(sagas)
将它们全部组合到 RootSaga 中。
一切正常,没有问题。我发现了 Sagas 内部的错误。但是,现在的要求也是在 RootSaga 级别捕获错误,无论如何,有人会错过捕获有问题的部分。(实际上,我正在为一个多团队项目编写样板文件。)
我明白了,如果有人没有使用 try catch 来捕捉有问题的部分,那么错误就会传播,Saga 之后就会完全停止工作。 Sagas 之后不会再看任何东西了。
我想做的是,让其他 Sagas 运行 没有问题,然后让 RootSaga 正常工作,继续照常观看。我怎样才能做到这一点?
export default function* rootSaga() {
yield all(sagas);
}
当您 运行 rootSaga
时,您正在取回一项任务。该任务有一个 done
属性 这是一个承诺。所以:
const rootSagaTask = reduxSagaMiddleware.run(rootSaga);
rootSagaTask.done.catch(error => {
// Error here is a fatal error.
// None of the sagas down the road caught it.
});
v1 使用
rootSagaTask.toPromise().catch(e => {
});
经过深思熟虑这个问题,我实现了自己的中间件功能。喜欢:
中间件函数:
* @function errorFallback
* @param {CallableFunction} sagaFunction
* @param {object} action
* @description
* * Captured exceptions which is occurred from saga function.
* * Whenever any exception occurred from saga functions, whole sagas will be destroyed.
* * We can use this function to save sagas from die.
* @example
* export function* getSagaData() {
* yield all([
* takeLatest('GET_SAGA_DATA', errorFallback, getSaga),
* ]);
* }
*/
function* errorFallback(sagaFunction, action) {
try {
yield sagaFunction(action);
} catch (error) {
// exception log to your server
}
}
export default errorFallback;
使用
export function* loadSampleSaga() {
try {
const response = yield post('/create', {
name: 'test',
salary: '123',
age: '23',
});
yield put(sampleSuccessAction());
} catch (err) {
yield put(sampleFailureAction());
throw err;
}
}
export function* getSampleSaga() {
yield all([takeLatest('GET_SAMPLE_SAGA_DATA', errorFallback, loadSampleSaga)]);
}
我正在组合来自不同模块的一组 Sagas(一些 takeEvery
和 takeLatest
),并使用 yield all(sagas)
将它们全部组合到 RootSaga 中。
一切正常,没有问题。我发现了 Sagas 内部的错误。但是,现在的要求也是在 RootSaga 级别捕获错误,无论如何,有人会错过捕获有问题的部分。(实际上,我正在为一个多团队项目编写样板文件。)
我明白了,如果有人没有使用 try catch 来捕捉有问题的部分,那么错误就会传播,Saga 之后就会完全停止工作。 Sagas 之后不会再看任何东西了。
我想做的是,让其他 Sagas 运行 没有问题,然后让 RootSaga 正常工作,继续照常观看。我怎样才能做到这一点?
export default function* rootSaga() {
yield all(sagas);
}
当您 运行 rootSaga
时,您正在取回一项任务。该任务有一个 done
属性 这是一个承诺。所以:
const rootSagaTask = reduxSagaMiddleware.run(rootSaga);
rootSagaTask.done.catch(error => {
// Error here is a fatal error.
// None of the sagas down the road caught it.
});
v1 使用
rootSagaTask.toPromise().catch(e => {
});
经过深思熟虑这个问题,我实现了自己的中间件功能。喜欢:
中间件函数:
* @function errorFallback
* @param {CallableFunction} sagaFunction
* @param {object} action
* @description
* * Captured exceptions which is occurred from saga function.
* * Whenever any exception occurred from saga functions, whole sagas will be destroyed.
* * We can use this function to save sagas from die.
* @example
* export function* getSagaData() {
* yield all([
* takeLatest('GET_SAGA_DATA', errorFallback, getSaga),
* ]);
* }
*/
function* errorFallback(sagaFunction, action) {
try {
yield sagaFunction(action);
} catch (error) {
// exception log to your server
}
}
export default errorFallback;
使用
export function* loadSampleSaga() {
try {
const response = yield post('/create', {
name: 'test',
salary: '123',
age: '23',
});
yield put(sampleSuccessAction());
} catch (err) {
yield put(sampleFailureAction());
throw err;
}
}
export function* getSampleSaga() {
yield all([takeLatest('GET_SAMPLE_SAGA_DATA', errorFallback, loadSampleSaga)]);
}