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
The Effect.orElseFail function will replace the original failure with a new failure value:
Example (Replacing Failure with Effect.orElseFail)
orElseSucceed
The Effect.orElseSucceed function will 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.