Search code examples
reactjsreact-reduxredux-saga

Redux Saga stops after error


I'm combining a set of Sagas, ( some takeEvery and takeLatest ) from different modules and using yield all(sagas) to combine them all in RootSaga.

Everything works, without issue. I catch errors inside the Sagas themselves. But, now the requirement is to catch errors at RootSaga level too, in any case, someone misses catching a problematic part.( Actually, I'm working on boilerplate for a multi-team project. )

I see, if someone is not using try catch to catch a problematic part, then the error is propagated and Saga completely stops working after that. Sagas won't watch anything thereafter.

What I want to do is, let other Sagas run without issues and RootSaga to be working thereafter, keep watching as usual. How can I achieve this?

export default function* rootSaga() {
    yield all(sagas);
}

Solution

  • When you run the rootSaga you are getting back a task. That task has a done property which is a promise. So:

    const rootSagaTask = reduxSagaMiddleware.run(rootSaga);
    
    rootSagaTask.done.catch(error => {
      // Error here is a fatal error.
      // None of the sagas down the road caught it.
    });