appcues / mojito

An easy-to-use Elixir HTTP client, built on the low-level Mint library.
https://hexdocs.pm/mojito/Mojito.html
MIT License
349 stars 34 forks source link

on error sends me a message as well as return value #100

Closed brianmay closed 2 years ago

brianmay commented 2 years ago

My understanding is that all I need to do to handle errors is look at the result, e.g.

case Mojito.get(url, headers) do
    {:ok, %Mojito.Response{status_code: 200, body: body}} ->
      {:ok, Jason.decode!(body)}

    {:ok, %Mojito.Response{status_code: status_code}} ->
      {:error, "Unexpected response #{status_code}"}

    {:error, error} ->
      {:error, error}
 end

But I find on certain types of errors, I also have to implement the appropriate handle_info call.

e.g. I got the error:

{:error, %Mojito.Error{message: nil, reason: :timeout}}

But my genserver died also, about 0.2 seconds later.

** (FunctionClauseError) no function clause matching in PenguinNodes.Life360.Circles.handle_info/2
    (penguin_nodes 0.1.0) lib/penguin_nodes/life360/circles.ex:79: PenguinNodes.Life360.Circles.handle_info({:mojito_response, #Reference<0.1958781291.3315859457.252057>, {:ok, %Mojito.Response{...}], size: 328, status_code: 200}}}, %PenguinNodes.Nodes.NodeModule.State{...}})

What seems strange is that this looks like a valid response. So maybe the problem is that mojito timed out, and then we got a response and mojito is confused by this response after it already timed out?

Am I expected to receive messages from mojito too (if so this should be documented in README.md)? Or is this message a bug?

andyleclair commented 2 years ago

Hi Brian, thanks for the detailed report! I do think this is a bug (and most likely related to https://github.com/appcues/mojito/issues/86)

I'm going to try to gin up a test case to try to reproduce what's happening locally to see if I can figure it out

brianmay commented 2 years ago

Some extra data points, for most recent experiences:

I said 0.2 seconds, wonder if I misread that. Also some of these might be post requests (with relatively small data payload containing username/password) not get requests. Doubt it matters, everything else is the same.

andyleclair commented 2 years ago

I'd suggest that for the time being, you try a different client, mostly for the reasons stated here: https://elixirforum.com/t/mint-vs-finch-vs-gun-vs-tesla-vs-httpoison-etc/38588/11

We (Appcues) are in the process of switching to use Finch in basically all cases now, it just performs better

brianmay commented 2 years ago

Thanks for the link, it is probably good to keep my options open. Will try Finch.

ntenczar commented 2 years ago

Hi! This project is now deprecated:

We recommend that you use Finch which is also built on Mint. The creator of Finch has an excellent writeup here describing the problems with Mojito, and as a result we use Finch internally at Appcues now.