safwank / ElixirRetry

Simple Elixir macros for linear retry, exponential backoff and wait with composable delays
Other
441 stars 32 forks source link

Specify only _some_ errors to be retried. #54

Open rafal0p opened 1 year ago

rafal0p commented 1 year ago

Let's assume that we have a function like that:

  @spec foo() ::
          :ok
          | {:error, :some_retryable_reason}
          | {:error, :other_retryable_reason}
          | {:error, :some_non_retryable_reason}
          | {:error, :other_non_retryable_reason}
  def foo() do
    ...
  end

What I'd like to express is roughly this:

retry with: linear_backoff(50, 1) |> take(5),
      rescue_only: [
        {:error, :some_retryable_reason},
        {:error, :other_retryable_reason}
      ] do
  foo()
after
  _ -> :ok
else
  error -> error
end

So - for some subset of errors attempt the retry, while for others give up instantly.

Currently it is impossible:

nathanalderson commented 1 year ago

What if it accepted a list of patterns? I would love this for Exceptions as well. I'm willing to try a PR in that direction if interested.

rafal0p commented 1 year ago

Could you give an example of such API?