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

JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XX...
New

We're in Beta

About us Mission Statement