I'm trying to create an error interceptor for all my sagas, after analyzing the redux-saga eventChannel I tried to create a saga like this:
export function interceptor() {
return eventChannel(() => {
api.interceptors.response.use(
(response) => response,
(error) => {
const { response } = error;
if ([401, 403].includes(response.status)) {
emit(AuthCreators.logoutRequest());
}
return Promise.reject(error);
}
);
return () => null;
});
}
In rootSaga it is being called this way:
export default function* rootSaga() {
return yield all([fork(interceptor), anotherSaga, anotherSaga2]);
}
This way, every time one of my other sagas has a catch the interceptor is triggered, however my emit that should trigger the logoutRequest that is in other saga is not being triggered.
Already grateful
Maybe I don't understand you question exactly but this might help you
const saga = [
anotherSaga, anotherSaga2
]
yield all(sagas.map((saga) => spawn(function* () {
try {
yield call(saga);
} catch (e) {
// console.log(e);
// here we should store all errors in some service...
}
})));