Skip to content

Commit fc0c4cc

Browse files
author
Ashu Goel
committed
[endpoint] gracefully handle httpoison errors
1 parent 1689a98 commit fc0c4cc

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

lib/endpoint.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ defmodule SparkPost.Endpoint do
8686
end
8787
end
8888

89+
defp handle_response({:error, %HTTPoison.Error{reason: reason}}, _decode_results) do
90+
%SparkPost.Endpoint.Error{status_code: nil, errors: [reason]}
91+
end
92+
8993
defp base_request_headers() do
9094
{:ok, version} = :application.get_key(:sparkpost, :vsn)
9195
%{

lib/mockserver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ defmodule SparkPost.MockServer do
3636
def mk_http_resp(status_code, body) do
3737
fn (_method, _url, _body, _headers, _opts) -> {:ok, %HTTPoison.Response{status_code: status_code, body: body}} end
3838
end
39+
40+
def mk_error(reason) do
41+
fn (_method, _url, _body, _headers, _opts) -> {:error, %HTTPoison.Error{reason: reason}} end
42+
end
3943
end

test/endpoint_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,14 @@ defmodule SparkPost.EndpointTest do
102102
Endpoint.request(:get, "transmissions", %{}, %{}, [])
103103
end
104104
end
105+
106+
test_with_mock "Endpoint request can handle httpoison timeouts", HTTPoison,
107+
[request: fn (method, url, body, headers, opts) ->
108+
fun = MockServer.mk_error(:timeout)
109+
fun.(method, url, body, headers, opts)
110+
end
111+
] do
112+
assert %Endpoint.Error{errors: [:timeout], status_code: nil, results: nil} ==
113+
Endpoint.request(:post, "transmissions", %{}, %{}, [])
114+
end
105115
end

0 commit comments

Comments
 (0)