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

Tee
can someone please explain to me how Enum.reduce works with maps
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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

We're in Beta

About us Mission Statement