When we run an Effect
we get back the success type (first type parameter).
But what happens if we don't handle one of the expected errors (second type parameter)?
const main = fetchRequest.pipe(
Effect.flatMap(jsonResponse),
Effect.catchTags({
FetchError: () => Effect.succeed("Fetch error"),
// JsonError: () => Effect.succeed("Json error"),
})
);
We are not recovering from JsonError
anymore. If a JsonError
does occur now effect will throw.
We can avoid this situation by using runPromiseExit
instead of runPromise
:
const main = fetchRequest.pipe(
Effect.flatMap(jsonResponse),
Effect.catchTags({
FetchError: () => Effect.succeed("Fetch error"),
// JsonError: () => Effect.succeed("Json error"),
})
);
Effect.runPromiseExit(main);
runPromiseExit
instead of throwing returns Exit
:
export type Exit<A, E = never> = Success<A, E> | Failure<A, E>
When running an Effect<A, E>
, Exit
contains either a Success<A>
(first type parameter) or a Failure<E>
.
Specifically, the Failure
returned by runPromiseExit
contains a Cause<E>
:
export type Cause<E> = Empty | Fail<E> | Die | Interrupt | Sequential<E> | Parallel<E>
When we don't recover from an error and running the effect fails
Cause
will contains aFail<E>
.
Cause
then allows to deal with unexpected errors.
Let's see how next.