# `ExResilience.Fallback`
[🔗](https://github.com/joshrotenberg/ex_resilience/blob/v0.4.0/lib/ex_resilience/fallback.ex#L1)

Stateless fallback layer for graceful degradation.

When the wrapped function returns an error, calls a fallback function
instead. The fallback receives the error result and returns a replacement
value.

## Options

  * `:name` -- optional atom for telemetry metadata. Default `:fallback`.
  * `:fallback` -- required 1-arity function receiving the error result,
    returns the fallback value.
  * `:only` -- optional 1-arity predicate; fallback only triggers when
    this returns `true`. Default: matches `{:error, _}` and `:error`.
    Takes precedence over `:error_classifier` when both are provided.
  * `:error_classifier` -- module implementing `ExResilience.ErrorClassifier`.
    When provided (and `:only` is not), triggers fallback for results
    classified as `:retriable` or `:failure`. Ignored if `:only` is also set.

## Examples

    iex> ExResilience.Fallback.call(fn -> {:error, :down} end, fallback: fn _err -> {:ok, :cached} end)
    {:ok, :cached}

    iex> ExResilience.Fallback.call(fn -> {:ok, 42} end, fallback: fn _err -> {:ok, 0} end)
    {:ok, 42}

# `option`

```elixir
@type option() ::
  {:name, atom()}
  | {:fallback, (term() -&gt; term())}
  | {:only, (term() -&gt; boolean())}
  | {:error_classifier, module()}
```

# `call`

```elixir
@spec call((-&gt; term()), [option()]) :: term()
```

Executes `fun` and applies the fallback if the result matches the error predicate.

The `:fallback` option is required and must be a 1-arity function that receives
the error result.

## Examples

    iex> ExResilience.Fallback.call(
    ...>   fn -> {:error, :timeout} end,
    ...>   fallback: fn {:error, reason} -> {:ok, {:default, reason}} end
    ...> )
    {:ok, {:default, :timeout}}

    iex> ExResilience.Fallback.call(
    ...>   fn -> {:ok, :fast} end,
    ...>   fallback: fn _ -> {:ok, :slow} end
    ...> )
    {:ok, :fast}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
