Skip to content

don't swallow stack traces #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ result = retry with: constant_backoff(100) |> Stream.take(10) do
after
result -> result
else
e when is_exception(e) -> raise e
{e, stacktrace} when is_exception(e) -> reraise e, stacktrace
e -> e
end
```
Expand Down
8 changes: 4 additions & 4 deletions lib/retry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Retry do
default: [
else:
quote do
e when is_exception(e) -> raise e
{e, stacktrace} when is_exception(e) -> reraise e, stacktrace
e -> e
end,
after:
Expand Down Expand Up @@ -162,8 +162,8 @@ defmodule Retry do
fun.()
end)
|> case do
{:exception, e} ->
case e do
{:exception, e, stacktrace} ->
case {e, stacktrace} do
unquote(else_clause)
end

Expand Down Expand Up @@ -336,7 +336,7 @@ defmodule Retry do
rescue
e ->
if e.__struct__ in unquote(exceptions) do
{:cont, {:exception, e}}
{:cont, {:exception, e, __STACKTRACE__}}
else
reraise e, __STACKTRACE__
end
Expand Down
21 changes: 19 additions & 2 deletions test/retry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ defmodule RetryTest do
defmodule(CustomError, do: defexception(message: "custom error!"))

describe "retry" do
defp boom, do: raise "boom"

test "keeps correct stack trace" do
e =
try do
retry with: [1] do
boom()
end
rescue
e ->
Exception.format(:error, e, __STACKTRACE__)
end

assert e =~ "(RuntimeError) boom"
assert e =~ "test/retry_test.exs:12: RetryTest.boom"
end

test "retries execution for specified attempts when result is error tuple" do
{elapsed, _} =
:timer.tc(fn ->
Expand Down Expand Up @@ -94,7 +111,7 @@ defmodule RetryTest do
after
_ -> :ok
else
error -> raise error
{error, _} -> raise error
end
end
end)
Expand All @@ -113,7 +130,7 @@ defmodule RetryTest do
after
_ -> :ok
else
error -> raise error
{error, _} -> raise error
end
end
end)
Expand Down