Error handling
How to manage errors
By leveraging effection
and structured concurrency
we can let it do most of the heavy lifting for managing errors.
Read error handling doc at
effection
!
There are some tools starfx
provides to make it a little easier.
By default in effection
, if a child task raises an exception, it will bubble
up the ancestry and eventually try to kill the root task. Within starfx
, we
prevent that from happening with supervisor tasks. Having said
that, child tasks can also control how children tasks are managed. Sometimes you
want to kill the child task tree, sometimes you want to recover and restart, and
sometimes you want to bubble the error up the task ancestry.
If you want to capture a task and prevent it from bubbling an exception up, then
you have two fx
: call
and safe
.
1import { call, run, safe } from "starfx";
2
3function* main() {
4 try {
5 // use `call` to enable JS try/catch
6 yield* call(fetch("api.com"));
7 } catch (err) {
8 console.error(err);
9 }
10
11 // -or- if you don't want to use try/catch
12 const result = yield* safe(fetch("api.com"));
13 if (!result.ok) {
14 console.error(result.err);
15 }
16}
17
18await run(main);
Both functions will catch the child task and prevent it from bubbling up the error.