0
votes

I'm currently building a checkers game in phoenix that's hitting into a "FunctionClauseError". The error is being thrown by my "handle_in" function in the games channel which I've provided here:

def handle_in("click", %{"click" => ll}, socket) do
    IO.puts("whatever")
end

Any ideas as to what we're doing wrong?

1
What's in the function clause error? It sounds like handle_in/3 is getting called with different arguments. Are you handling the general case there? - Brett Beatty
Could you post more details about the error message? As pointed out above, it sounds like a function is being invoked with different arguments than it accepts. I don't think the problem is with the click function, because you're passing 2 arguments in every call that's shown. But it could be in handle_in if it's being called with something other than "click" or a map that doesn't contain the "click" key. - sbacarob
The error is not caused by handle_in, but by the code that calls handle in. I edited your question to remove the unrelated information. Please post the full errror message and the code that calls handle_in. - Adam Millerchip
Change it like this to debug def handle_in("click", payload, socket), do: IO.puts(payload) ` - Vasu Adari

1 Answers

1
votes

Here's an example:

defmodule Example do
  def run() do
    Demo.handle_in("UNEXPECTED", "foo", "bar")
  end

  def handle_in("click", map, socket) do
    IO.puts("whatever")
  end
end

And here's the error message when we call run/0:

iex(1)> Example.run
** (FunctionClauseError) no function clause matching in Example.handle_in/3

    The following arguments were given to Example.handle_in/3:

        # 1
        "UNEXPECTED"

        # 2
        "foo"

        # 3
        "bar"

You can see from this output, that run/1 calls handle_in/3 with "UNEXPECTED" as its first argument. There is no clause of handle_in that expects that, so elixir generates the error. A common way to handle this if you can't control the inputs is to add a catch-all clause that does not pattern-match the arguments:

def handle_in("click", map, socket) do
  IO.puts("clicked")
end

def handle_in(one, two, three) do
  IO.puts("Called with: #{inspect one}, #{inspect two}, #{inspect three}")
end

Now the error is not produced, because the second clause can handle the "UNEXPECTED" string:

iex(1)> Example.run
Called with: "UNEXPECTED", "foo", "bar"
:ok

In your case, it could be that the "click" string is not passed, or that the 2nd argument isn't a map that contains the "click" key, but without the calling code or error message, it's impossible to tell.