Skip to content

Commit 276ee6e

Browse files
committed
Raise ArgumentError in Keyword.keys when list is not a keyword list
Previously it would return something like this: ** (FunctionClauseError) no function clause matching in anonymous fn/1 in Keyword.keys/1 The following arguments were given to anonymous fn/1 in Keyword.keys/1: # 1 {"fetch/2", {:fetch, 2}} Closes #10010, and it improves 3fd68ef
1 parent 323dac0 commit 276ee6e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

lib/elixir/lib/keyword.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,29 @@ defmodule Keyword do
431431
432432
iex> Keyword.keys(a: 1, b: 2)
433433
[:a, :b]
434+
434435
iex> Keyword.keys(a: 1, b: 2, a: 3)
435436
[:a, :b, :a]
436437
438+
iex> Keyword.keys([{:a, 1}, {"b", 2}, {:c, 3}])
439+
** (ArgumentError) expected a keyword list, but an element in the list is not a keyword; got: {"b", 2}
440+
437441
"""
438442
@spec keys(t) :: [key]
439443
def keys(keywords) when is_list(keywords) do
440-
:lists.map(fn {k, _} when is_atom(k) -> k end, keywords)
444+
:lists.map(
445+
fn
446+
{key, _} when is_atom(key) ->
447+
key
448+
449+
element ->
450+
raise ArgumentError,
451+
"expected a keyword list, but an element in the list is not a keyword; got: #{
452+
inspect(element)
453+
}"
454+
end,
455+
keywords
456+
)
441457
end
442458

443459
@doc """

0 commit comments

Comments
 (0)