Skip to content

Commit

Permalink
Merge pull request #3 from secretworry/master
Browse files Browse the repository at this point in the history
Carries the env back after the async request failed
  • Loading branch information
zhongwencool committed Jul 29, 2016
2 parents 4dc712b + 27d688d commit 82e2bbe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ If the adapter supports it, you can make asynchronous requests by passing `respo
Maxwell.get(url: "http://example.org", respond_to: self)

receive do
{:maxwell_response, res} -> res.status # => 200
{:maxwell_response, {:ok, res}} -> res.status # => 200
{:maxwell_response, {:error, reason, env}} -> env # the request env
end
```

Expand Down
10 changes: 6 additions & 4 deletions lib/maxwell/adapter/hackney.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ defmodule Maxwell.Adapter.Hackney do
"""
def call(env) do
if target = env.opts[:respond_to] do
env = if target = env.opts[:respond_to] do
gatherer = spawn_link fn -> receive_response(env, target, nil , nil, nil) end
opts = env.opts |> List.keyreplace(:respond_to, 0, {:stream_to, gatherer})
env = %{env |opts: [:async| opts]}
%{env |opts: [:async| opts]}
else
env
end

env
|> send_req
|> format_response(env)
end
Expand Down Expand Up @@ -60,8 +62,8 @@ defmodule Maxwell.Adapter.Hackney do
receive_response(env, target, new_status, headers, body)
{:hackney_response, _id, {:headers, new_headers}} ->
receive_response(env, target, status, new_headers, body)
{:hackney_response, _id, {:error, _reason} = error} ->
send(target, {:maxwell_response, error})
{:hackney_response, _id, {:error, reason}} ->
send(target, {:maxwell_response, {:error, reason, env}})
{:hackney_response, _id, :done} ->
response =
{:ok, status, headers, body}
Expand Down

0 comments on commit 82e2bbe

Please sign in to comment.