[Question] Parent catching forked tasks errors?
See original GitHub issueI’d like to do something like this:
function* saga() {
const forkedTasks = [...];
try {
// Wait for all forked tasks (is there a more idiomatic way? maybe `join` ?)
yield forkedTasks.map(task => task.done);
}
catch (e) {
yield put(displayError());
throw e;
}
}
But it does not work. The parent seems to be cancelled and never get the opportunity to catch the error.
Errors from child tasks automatically bubble up to their parents. If any forked task raises an uncaught error, then the parent task will abort with the child Error, and the whole Parent’s execution tree (i.e. forked tasks + the main task represented by the parent’s body if it’s still running) will be cancelled.
So, when using forked tasks, is there a way for the parent to catch a child error? And eventually decide weither or not to kill the whole tasks tree?
The following seems to work, but it feels a bit unnatural and error prone to me. It’s quite easy when using spawn
to get tricked and let tasks alive while they should dye.
function* saga() {
const spawnedTasks = [...];
try {
yield spawnedTasks.map(task => task.done);
}
catch (e) {
yield put(displayError());
throw e;
}
finally {
yield spawnedTasks.map(task => cancel(task));
}
}
Any idea on how this “error interception” pattern (I often want to do that) can be more idiomatically implemented?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:8 (8 by maintainers)
Top GitHub Comments
You can wrap your forks with the desired Error handler
closing this. I think more finer-grained error control is needed. will use #570 for tracking this