Skip to content

Commit 0d30dbf

Browse files
author
José Valim
committed
Properly annotate key errors for structs, closes #7935
1 parent 793bbe6 commit 0d30dbf

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/elixir/lib/exception.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ defmodule KeyError do
11631163
end
11641164

11651165
defp map_with_atom_keys_only?(term) do
1166-
is_map(term) and Enum.all?(term, fn {k, _} -> is_atom(k) end)
1166+
is_map(term) and Enum.all?(Map.to_list(term), fn {k, _} -> is_atom(k) end)
11671167
end
11681168

11691169
defp available_keys(term) when is_map(term), do: Map.keys(term)

lib/elixir/test/elixir/exception_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ defmodule ExceptionTest do
510510
"""
511511
end
512512

513+
test "annotates key error with suggestions for structs" do
514+
message = blame_message(%URI{}, fn map -> map.schema end)
515+
assert message =~ "key :schema not found in: %URI{"
516+
assert message =~ "Did you mean one of:"
517+
assert message =~ "* :scheme"
518+
end
519+
513520
if :erlang.system_info(:otp_release) >= '21' do
514521
test "annotates +/1 arithmetic errors" do
515522
assert blame_message(:foo, &(+&1)) == "bad argument in arithmetic expression: +(:foo)"

0 commit comments

Comments
 (0)