This page explains various techniques for handling failures and creating fallback mechanisms in the Effect library.
orElse
We can attempt one effect, and if it fails, try another effect using the Effect.orElse function.
Example (Handling Fallback with Effect.orElse)
orElseFail / orElseSucceed
These two operators modify the outcome of a failure by either replacing it with a constant failure or success value.
The Effect.orElseFail will always replace the original failure with a new failure value:
Example (Replacing Failure with Effect.orElseFail)
The Effect.orElseSucceed function will always replace the original failure with a success value, ensuring the effect cannot fail:
Example (Replacing Failure with Success using Effect.orElseSucceed)
firstSuccessOf
The Effect.firstSuccessOf function helps run a series of effects and returns the result of the first successful one. If none of the effects succeed, it fails with the error from the last effect in the series.
Example (Finding Configuration with Fallbacks)
In this example, we try to retrieve a configuration from different nodes. If the primary node fails, we fall back to other nodes until we find a successful configuration.