safwank / ElixirRetry

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

Make after & else clauses optional #55

Closed nathanalderson closed 1 year ago

nathanalderson commented 1 year ago

This PR makes the after and else clauses optional. Essentially, this:

retry with: ... do
  ...
end

Is now equivalent to:

retry with: ... do
  ...
after
  result -> result
else
  error -> raise error
end

I could be convinced to change the default else behavior to simply returning the exception, but re-raising it seems more natural to me.

I also improved the error messaging slightly by calling out the specific option or clause the user messed up when possible. So you'll get a message like you must provide a "do" clause or option "foo" is unsupported instead of a more generic message.

So far I have only done the implementation for the retry macro, but if you like the concept, I will update wait similarly. I think retry_while is already fine in this respect since it only has a do block.

safwank commented 1 year ago

Thanks for the PR @nathanalderson! I've been wanting to do this for a while. I'll take a look at the PR this weekend and get back to you.

nathanalderson commented 1 year ago

Went ahead and did wait. This:

wait ... do
  ...
end

Is now equivalent to this:

wait ... do
  ...
after
  result -> {:ok, result}
else
  error -> {:error, error}
end

A few other notes:

nathanalderson commented 1 year ago

After some thought, I realized that the appropriate thing to do in the default retry ... else clause is to reraise if the last failure was an exception and return the failed value otherwise. That's now the behavior. In other words:

retry with: ... do
  ...
end

Is now equivalent to:

retry with: ... do
  ...
after
  result -> result
else
  e when is_exception(e) -> raise e
  e -> e
end
nathanalderson commented 1 year ago

Sorry, meant to mark this as ready. Let me know if you have feedback.

I have a follow-up PR that addresses some more needs I have including some discussed in #54.

nathanalderson commented 1 year ago

Just bumping this a bit. Not super urgent but didn't want it to fall off your radar or something. Let me know if there's anything I can do to assist.

safwank commented 1 year ago

@nathanalderson Rest assured I haven't forgotten about this. I'm currently busy with life, so please bear with me.

nathanalderson commented 1 year ago

Periodic bump. Would love to get this in and introduce a follow-up. 🙂

safwank commented 1 year ago

@nathanalderson Thanks for the fixes. A couple of comments

nathanalderson commented 1 year ago

Sorry, the merge commit was accidental. That's the follow-up PR I've mentioned. Meant to merge upstream/master instead of origin/master. I've reverted. I also covered the two missed syntax errors in unit test.

I also added a comment to disable the complexity warning from credo on the main retry macro. I tried moving the else/after logic into a function and that did fix the warning, but in my opinion it made things less readable. I'm open to doing that if you prefer.