Skip to content

Commit

Permalink
Properly annotate key errors for structs, closes #7935
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
  • Loading branch information
José Valim committed Jul 19, 2018
1 parent c5e0704 commit dc25bb8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/elixir/lib/exception.ex
Expand Up @@ -1161,7 +1161,7 @@ defmodule KeyError do
end

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

defp available_keys(term) when is_map(term), do: Map.keys(term)
Expand Down
7 changes: 7 additions & 0 deletions lib/elixir/test/elixir/exception_test.exs
Expand Up @@ -504,6 +504,13 @@ defmodule ExceptionTest do
"""
end

test "annotates key error with suggestions for structs" do
message = blame_message(%URI{}, fn map -> map.schema end)
assert message =~ "key :schema not found in: %URI{"
assert message =~ "Did you mean one of:"
assert message =~ "* :scheme"
end

if :erlang.system_info(:otp_release) >= '21' do
test "annotates +/1 arithmetic errors" do
assert blame_message(:foo, &(+&1)) == "bad argument in arithmetic expression: +(:foo)"
Expand Down

0 comments on commit dc25bb8

Please sign in to comment.