chadcf

chadcf

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!

Most Liked

NobbZ

NobbZ

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.

wojtekmach

wojtekmach

Hex Core Team

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.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement