Skip to content

Commit 9297a91

Browse files
bsmrJosé Valim
authored and
José Valim
committed
fix for Erlang R18.0 changes in maps module
Signed-off-by: José Valim <[email protected]>
1 parent 0410a1c commit 9297a91

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/elixir/lib/exception.ex

+28-1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,14 @@ defmodule BadStructError do
564564
end
565565
end
566566

567+
defmodule BadMapError do
568+
defexception [map: nil]
569+
570+
def message(exception) do
571+
"expected a map, got: #{inspect(exception.map)}"
572+
end
573+
end
574+
567575
defmodule MatchError do
568576
defexception [term: nil]
569577

@@ -669,7 +677,12 @@ defmodule KeyError do
669677
defexception key: nil, term: nil
670678

671679
def message(exception) do
672-
"key #{inspect exception.key} not found in: #{inspect exception.term}"
680+
msg = "key #{inspect exception.key} not found"
681+
if exception.term != nil do
682+
msg <> " in: #{inspect exception.term}"
683+
else
684+
msg
685+
end
673686
end
674687
end
675688

@@ -767,6 +780,20 @@ defmodule ErlangError do
767780
def normalize({:badmatch, term}, _stacktrace) do
768781
%MatchError{term: term}
769782
end
783+
784+
# Erlang R18.0 changed this for various functions in the maps module
785+
def normalize({:badmap, map}, _stacktrace) do
786+
%BadMapError{map: map}
787+
end
788+
789+
# Erlang R18.0 changed this for various functions in the maps module
790+
def normalize({:badkey, key}, stacktrace) do
791+
term = case stacktrace do
792+
[{:maps, :update, [_, _, map], []}|_] -> map
793+
_ -> nil
794+
end
795+
%KeyError{key: key, term: term }
796+
end
770797

771798
def normalize({:case_clause, term}, _stacktrace) do
772799
%CaseClauseError{term: term}

lib/elixir/test/elixir/map_test.exs

+11-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,17 @@ defmodule MapTest do
8484

8585
test "update maps" do
8686
assert %{two_items_map | a: 3} == %{a: 3, b: 2}
87-
88-
assert_raise ArgumentError, fn ->
89-
%{two_items_map | c: 3}
87+
88+
# TODO: proper handling of API changes in different Erlang/OTP releases
89+
case :erlang.system_info(:otp_release) do
90+
'17' ->
91+
assert_raise ArgumentError, fn ->
92+
%{two_items_map | c: 3}
93+
end
94+
_ ->
95+
assert_raise KeyError, fn ->
96+
%{two_items_map | c: 3}
97+
end
9098
end
9199
end
92100

0 commit comments

Comments
 (0)