fireproofsocks

fireproofsocks

Understanding Dialyzer error: The pattern can never match the type

I’m using Dialyzer to check over some code that uses the mongodb package. I’ve got a function that abstracts the Mongo.insert_one/4 function and adds in some extra logging for certain conditions:

  def insert(collection, args) do
    case Mongo.insert_one(mongo_pid(), collection, args) do
      {:ok, %Mongo.InsertOneResult{acknowledged: true, inserted_id: id}} ->
        {:ok, %{"_id" => id}}

      # Check for 11000 error (usually this means a duplicate)
      {:error, %Mongo.WriteError{write_errors: [%{"code" => 11_000} | _]} = err} -> {:error, "Duplicate entry"}

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

When I run dialyzer, I get the following error:

The pattern can never match the type.

Pattern:
{:error, _err = %Mongo.WriteError{:write_errors => [%{<<99, 111, 100, 101>> => 11000} | _]}}

Type:

  :ok
  | {:error,
     %Mongo.Error{
       :__exception__ => _,
       :code => number(),
       :host => binary(),
       :message => binary()
     }}
  | {:ok,
     %Mongo.InsertOneResult{
       :acknowledged => false,
       :inserted_id => nil | %BSON.ObjectId{:value => <<_::96>>}
     }}

After looking at the mongodb source code I can see that it appears to miss a viable return type:

@type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()}

should be (I think) something like:

@type result(t) :: :ok | {:ok, t} | {:error, Mongo.Error.t()} | {:error, Mongo.WriteError.t()}

My questions are:

  1. Am I correct in thinking that the dialyzer error in my app is resulting from what appears to be an incorrect type definition in a dependency (mongodb in this case)?
  2. Apart from filing an issue, what are the ways to configure dialyzer to ignore that particular error in that specific place?
  3. Why doesn’t Dialyzer catch this problem in mongodb? I cloned the repo, I fired up Dialyzer, and it shows no errors. Is that expected? Common?

Most Liked

michallepicki

michallepicki

  1. It does seem like the function spec for insert_one does not contain {:error, %Mongo.WriteError{}} in the return type. Whether their result type should be updated or not, I’m not sure. Notice that I wrote {:error, %Mongo.WriteError{}} and not {:error, Mongo.WriteError.t()} - I checked and there is no @type t defined in that module so it does not mean anything. This module got introduced in this commit and specs were not updated.
  2. It depends on how you’re running dialyzer. If you run the standalone dialyzer executable, you can use the @dialyzer module attribute. If you use dialyxir, their README.md lists a few more ways to ignore warnings. To me, the ignore file with string matches seems to be the most commonly used way to do that.
  3. Dialyzer accepts flags which configure what kinds of errors are shown. This kind of error would be shown if you used the -Woverspecs option. It is opt-in because it can introduce a lot of noise. Dialyzer has its limits and does not know how to deal with very complex types so it may sometimes “round up” some types to more general ones. I think Elixir’s with may be sometimes confusing it as well? I’m not sure. Adding specs to private functions used may help it. But overall, Dialyzer doesn’t guarantee to find all problems in your code.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; 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 Postgres...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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 forese...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement