OmegaNalphA
I’m having an issue getting my Dialyzer to agree with my output, and can’t seem to figure it out. Essentially, the response is {:error, %Mint.HTTP2{}, %Mint.HTTPError{}} instead of just {:error, _} and I can’t get the spec to reflect that.
This is the issue I’m facing, I keep getting an error from my Mint transport service (which is a whole separate issue) but I keep getting alerts that my handle_response isn’t able to match the type. After looking into it more the error tuple has two arguments in the shape of: {:error, %Mint.HTTP2{}, %Mint.HTTPError{}}.
Here is my handle_response:
@spec handle_response(
{:ok, [map()] | function()}
| {:error, String.t()}
| {:error, %Mint.HTTP2{}, %Mint.HTTPError{}},
Keyword.t()
) ::
{:ok, [map()] | function()} | {:error, term()}
defp handle_response(resp, opts)
defp handle_response({:ok, stream} = resp, stream: true) when is_function(stream), do: resp
defp handle_response({:ok, resp}, opts) when is_binary(resp),
do: handle_response({:ok, Jason.decode!(resp)}, opts)
defp handle_response({:ok, resp}, _), do: {:ok, Map.get(resp, "choices", [])}
defp handle_response({:error, _} = err, _), do: err
defp handle_response({:error, http, http_error}, _)
when is_map(http) and is_map(http_error),
do: {:error, http_error}
But I keep getting an error from my dialyzer here:
lib/broadcast.ex:129:pattern_match
The pattern can never match the type.
Pattern:
{:error, _http, _http_error}, _
Type:
{:error, _} | {:ok, _}, [{:stream, _}, ...]
I’m not sure why, I’m pretty sure the spec should be correct. Can anyone see what would be incorrect about this or does this seem reasonable?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OmegaNalphA
After searching more, I found this open issue that “solves” the problem I’m facing by downgrading the protocol to http1. Mint adapter cannot upload more than 65535 bytes on HTTP/2 · Issue #394 · elixir-tesla/tesla · GitHub
That being said, still not sure why my dialyzer is not working
al2o3cr
Can you show the code that calls
handle_response? The error message suggests that Dialyzer has inferred that the{:error, map(), map()}case “can’t happen” despite it being observed in production.OmegaNalphA
Definitely! The code is called here:
In the create_completion function
The client
handle_responseI’m realizing doing this it might be from the handle_response of the client, maybe I need to update that one?
al2o3cr
(assuming that
do_generateand thehandle_responsefrom your initial post are in the same module)Dialyzer is telling you the
{:error, _, _}branch inhandle_responsecan’t be reached becausecreate_completion’s return type matchesClient.handle_response({:ok, body()} | {:error, term()})As shown,
Client.handle_responsewill crash ifClient.postreturns{:error, _, _}, since it doesn’t have a matching clause. You’ll likely need to update:resultinClient, if it doesn’t include the 3-tuple case alreadyClient.handle_response, if you want to handle the 3-tuple case in the caller instead of inClientLostKobrakai
One thing to understand with dialyzer is that it starts out not caring about your spec at all.
Dialyzer does type inference. And for the given functions it inferred that the input will never be a tuple-3. Your function matches on such a tuple though, which is surfaced as an error.
The spec of the function is compared against inferred values and mismatches (as in the spec doesn’t have overlap with the inferred value) are reported. Iirc the overlap between the inferred type and the spec for the return type is then used as the input to later inferrence.
So an incorrect typespec can mess up validation on deeped down in the callstack function calls, but typespec cannot “widen” what would be considered a possible input to a function call compared to the inferred value.