Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure connection errors return the expected error tuple #577

Merged
merged 2 commits into from
May 6, 2023

Conversation

carrascoacd
Copy link
Contributor

@carrascoacd carrascoacd commented May 5, 2023

Bug

We encountered an in-the Mint adapter in Tesla. The error arises when we try to issue a new request but the connection was previously closed by the server. The response is a tuple of three elements, including the closed connection struct, but only two elements are expected in the behavior. In consequence, if we chain the Logger middleware this error happens: FunctionClauseError: no function clause matching in Tesla.Middleware.Logger.log_level/2

We can reproduce it by closing the connection before issuing the request, an example is:

Given this module

defmodule A do
  use Tesla

  adapter Tesla.Adapter.Mint
  
  plug Tesla.Middleware.Logger
  
  def get(), do: get("http://localhost:5984/ping")
end
iex(15)> A.get
** (FunctionClauseError) no function clause matching in Tesla.Middleware.Logger.log_level/2

    The following arguments were given to Tesla.Middleware.Logger.log_level/2:

        # 1
        {:error,
         %Mint.HTTP1{
           host: "localhost",
           port: 5984,
           request: nil,
           streaming_request: nil,
           socket: #Port<0.13>,
           transport: Mint.Core.Transport.TCP,
           mode: :active,
           scheme_as_string: "http",
           requests: {[], []},
           state: :closed,
           buffer: "",
           proxy_headers: [],
           private: %{},
           log: false
         }, %Mint.TransportError{reason: :closed}}

        # 2
        []

    Attempted function clauses (showing 2 out of 2):

        defp log_level({:error, _}, _)
        defp log_level({:ok, env}, config)

    (tesla 1.6.0) lib/tesla/middleware/logger.ex:213: Tesla.Middleware.Logger.log_level/2
    (tesla 1.6.0) lib/tesla/middleware/logger.ex:203: Tesla.Middleware.Logger.call/3

Proposed solution

  • Ensure we handle properly the connection errors. As example this should be logged:
iex(16)> A.get

    09:33:28.750 [error] GET http://localhost:5984/ping -> error: %Mint.TransportError{reason: :closed} (2.600 ms)
    {:error, %Mint.TransportError{reason: :closed}}
    
    09:33:28.750 [debug]
    >>> REQUEST >>>
    (no query)
    (no headers)
    (no body)
    
    <<< RESPONSE ERROR <<<
    %Mint.TransportError{reason: :closed}

PTAL @yordis. Thanks!

Copy link
Member

@yordis yordis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind bumping the version here:

@version "1.6.0"

@carrascoacd
Copy link
Contributor Author

Done!

@yordis yordis merged commit 30c3a29 into elixir-tesla:master May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants