Skip to content

Commit

Permalink
Add some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed May 24, 2024
1 parent eefd996 commit ea39bd5
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/req/response_async.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ defmodule Req.Response.Async do

def slice(_async), do: {:error, __MODULE__}

def reduce(_async, {:halt, acc}, _fun) do
def reduce(async, {:halt, acc}, _fun) do
cancel(async)
{:halted, acc}
end

Expand All @@ -49,7 +50,16 @@ defmodule Req.Response.Async do
message ->
case async.stream_fun.(async.ref, message) do
{:ok, [data: data]} ->
reduce(async, fun.(data, acc), fun)
result =
try do
fun.(data, acc)
rescue
e ->
cancel(async)
reraise e, __STACKTRACE__
end

reduce(async, result, fun)

{:ok, [:done]} ->
{:done, acc}
Expand All @@ -62,5 +72,9 @@ defmodule Req.Response.Async do
end
end
end

defp cancel(async) do
async.cancel_fun.(async.ref)
end
end
end

0 comments on commit ea39bd5

Please sign in to comment.