Skip to content

Commit 7113ccf

Browse files
committed
Provide better error message for keywords access with non-atom keys
1 parent 6660e7e commit 7113ccf

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/elixir/lib/access.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ defmodule Access do
6060
end
6161
end
6262

63+
def fetch(list, key) when is_list(list) do
64+
raise ArgumentError,
65+
"the Access calls for keywords expect the key to be an atom, got: " <> inspect(key)
66+
end
67+
6368
def fetch(nil, _key) do
6469
:error
6570
end

lib/elixir/test/elixir/access_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ defmodule AccessTest do
3636
assert Access.fetch([foo: :bar], :foo) == {:ok, :bar}
3737
assert Access.fetch([foo: :bar], :bar) == :error
3838

39-
assert_raise FunctionClauseError, fn ->
40-
Access.fetch([{"foo", :bar}], "foo")
39+
msg = ~r/the Access calls for keywords expect the key to be an atom/
40+
assert_raise ArgumentError, msg, fn ->
41+
Access.fetch([], "foo")
4142
end
4243

4344
assert Access.get([foo: :bar], :foo) == :bar

0 commit comments

Comments
 (0)