Handling http errors from HTTPoison / Hackney

I’m attempting to gracefully handle failures from an api call and am struggling a bit with how to do this. Take for example the following:

    case HTTPoison.get("https://url.com") do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        ...
      {:error, %HTTPoison.Error{} = error} ->
        ...
    end

Works great for one real world test case. What I’m struggling with is two things:

  1. Under what circumstances does HTTPoison return :error vs :ok? I can’t find this anywhere. I don’t know if it’s possible that it would ever return :ok with a status code of 500, or if it will always return :error on any status code > 400 or what… Do I need to match on both :error as well as :ok with a status code > 400 to catch failures?
  2. I cannot for the life of me figure out how to test this. We’re using Bypass in our tests, and I can have it return a Plug.Conn.resp with a 500 status code but the actual response still comes back with an :ok and not :error. I cannot for the life of me figure out how to get Bypass to return an :error response.

Any help is greatly appreciated!

1 Like

When the server sent a reply you’ll get an :ok tuple, if the request timed out, DNS couldn’t be resolved or otherwise a connection couldn’t be established, then you’ll see :error tuple returned, also if the reply sent by the server is not parsable as HTTP.

2 Likes

The underlying Hackney library can either return {:ok, status_code, headers, client_ref} or {:error, reason}. My interpretation of that is that you’ll get an :ok reply any time the connection is sufficiently successful to have a response code from the server, and :error when something goes so wrong (network failure, nonsensical server response, etc) that the response code isn’t available.

I cannot for the life of me figure out how to get Bypass to return an :error response.

See the example from README where Bypass.down/1 can be used to take the TCP server down, you should get an {:error, _} response then. Your mocked response may also do Process.sleep(:infinity), that should trigger a timeout somewhere too.

1 Like