You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Make after & else clauses optional for retry macro
* Run formatter
* Support flexible rescue_only parameters
* Support flexible atoms parameters
* Fix extra runs
* Add a few test cases
* Update documentation
* Fix unused variable warning
* Fix example code in docs
Copy file name to clipboardExpand all lines: README.md
+16-2
Original file line number
Diff line number
Diff line change
@@ -32,9 +32,23 @@ Check out the [API reference](https://hexdocs.pm/retry/api-reference.html) for t
32
32
33
33
The `retry([with: _,] do: _, after: _, else: _)` macro provides a way to retry a block of code on failure with a variety of delay and give up behaviors. By default, the execution of a block is considered a failure if it returns `:error`, `{:error, _}` or raises a runtime error.
34
34
35
-
An optional list of atoms can be specified in `:atoms` if you need to retry anything other than `:error` or `{:error, _}`, e.g. `retry([with: _, atoms: [:not_ok]], do: _, after: _, else: _)`.
35
+
Both the values and exceptions that will be retried can be customized. To control which values will be retried, provide the `atoms` option. To control which exceptions are retried, provide the `rescue_only` option. For example:
36
36
37
-
Similarly, an optional list of exceptions can be specified in `:rescue_only` if you need to retry anything other than `RuntimeError`, e.g. `retry([with: _, rescue_only: [CustomError]], do: _, after: _, else: _)`.
37
+
```
38
+
retry with: ..., atoms: [:not_ok], rescue_only: [CustomError] do
39
+
...
40
+
end
41
+
```
42
+
43
+
Both `atoms` and `rescue_only` can accept a number of different types:
44
+
45
+
* An atom (for example: `:not_okay`, `SomeStruct`, or `CustomError`). In this case, the `do` block will be retried in any of the following cases:
46
+
* The atom itself is returned
47
+
* The atom is returned in the first position of a two-tuple (for example, `{:not_okay, _}`)
48
+
* A struct of that type is returned/raised
49
+
* The special atom `:all`. In this case, all values/exceptions will be retried.
50
+
* A function (for example: `fn val -> String.starts_with?(val, "ok") end`) or partial function (for example: `fn {:error, %SomeStruct{reason: "busy"}} -> true`). The function will be called with the return value and the `do` block will be retried if the function returns a truthy value. If the function returns a falsy value or if no function clause matches, the `do` block will not be retried.
51
+
* A list of any of the above. The `do` block will be retried if any of the items in the list matches.
38
52
39
53
The `after` block evaluates only when the `do` block returns a valid value before timeout. On the other hand, the `else` block evaluates only when the `do` block remains erroneous after timeout. Both are optional. By default, the `else` clause will return the last erroneous value or re-raise the last exception. The default `after` clause will simply return the last successful value.
0 commit comments