You’ll need to have a second function clause for when connect is called without a token, the exception you’re seeing is because once you change the function params, it will only match calls where the params are a “map” with a “token” key in them, so any other call will be as if you didn’t have such a function (since the one you have doesn’t match). You’ll probably want to return an error tuple from it this second function definition.
I had a feeling that was the problem, but it I didn’t realize that function order is also important. I moved my param extract matcher above the default one, thanks.
BTW if I wanted to do this in a single function what would be the most elegant way of performing a case on the params with a “token” key? (and returning an :error if there is none).
The Elixir way would probably to avoid any conditional when function header pattern match could be used. As mentionned above, a second function matching the request without token can solve your problem.
With a case…
def connect(params, socket, _connect_info) do
case params do
%{"token" => token} -> ...
_ -> ...
end
end