@@ -177,6 +177,22 @@ defmodule Exception do
177177 end
178178 end
179179
180+ @ doc false
181+ @ spec _format_message_with_term ( String . t ( ) , any ) :: String . t ( )
182+ def _format_message_with_term ( message , term ) do
183+ inspected =
184+ term
185+ |> inspect ( pretty: true )
186+ |> String . split ( "\n " )
187+ |> Enum . map ( fn
188+ "" -> ""
189+ line -> " " <> line
190+ end )
191+ |> Enum . join ( "\n " )
192+
193+ message <> "\n \n " <> inspected
194+ end
195+
180196 @ doc """
181197 Attaches information to exceptions for extra debugging.
182198
@@ -1431,7 +1447,10 @@ defmodule BadStructError do
14311447
14321448 @ impl true
14331449 def message ( exception ) do
1434- "expected a struct named #{ inspect ( exception . struct ) } , got: #{ inspect ( exception . term ) } "
1450+ Exception . _format_message_with_term (
1451+ "expected a struct named #{ inspect ( exception . struct ) } , got:" ,
1452+ exception . term
1453+ )
14351454 end
14361455end
14371456
@@ -1451,7 +1470,10 @@ defmodule BadMapError do
14511470
14521471 @ impl true
14531472 def message ( exception ) do
1454- "expected a map, got: #{ inspect ( exception . term ) } "
1473+ Exception . _format_message_with_term (
1474+ "expected a map, got:" ,
1475+ exception . term
1476+ )
14551477 end
14561478end
14571479
@@ -1470,7 +1492,10 @@ defmodule BadBooleanError do
14701492
14711493 @ impl true
14721494 def message ( exception ) do
1473- "expected a boolean on left-side of \" #{ exception . operator } \" , got: #{ inspect ( exception . term ) } "
1495+ Exception . _format_message_with_term (
1496+ "expected a boolean on left-side of \" #{ exception . operator } \" , got:" ,
1497+ exception . term
1498+ )
14741499 end
14751500end
14761501
@@ -1492,7 +1517,10 @@ defmodule MatchError do
14921517
14931518 @ impl true
14941519 def message ( exception ) do
1495- "no match of right hand side value: #{ inspect ( exception . term ) } "
1520+ Exception . _format_message_with_term (
1521+ "no match of right hand side value:" ,
1522+ exception . term
1523+ )
14961524 end
14971525end
14981526
@@ -1518,7 +1546,10 @@ defmodule CaseClauseError do
15181546
15191547 @ impl true
15201548 def message ( exception ) do
1521- "no case clause matching: #{ inspect ( exception . term ) } "
1549+ Exception . _format_message_with_term (
1550+ "no case clause matching:" ,
1551+ exception . term
1552+ )
15221553 end
15231554end
15241555
@@ -1548,7 +1579,10 @@ defmodule WithClauseError do
15481579
15491580 @ impl true
15501581 def message ( exception ) do
1551- "no with clause matching: #{ inspect ( exception . term ) } "
1582+ Exception . _format_message_with_term (
1583+ "no with clause matching:" ,
1584+ exception . term
1585+ )
15521586 end
15531587end
15541588
@@ -1598,7 +1632,10 @@ defmodule TryClauseError do
15981632
15991633 @ impl true
16001634 def message ( exception ) do
1601- "no try clause matching: #{ inspect ( exception . term ) } "
1635+ Exception . _format_message_with_term (
1636+ "no try clause matching:" ,
1637+ exception . term
1638+ )
16021639 end
16031640end
16041641
@@ -2160,7 +2197,10 @@ defmodule KeyError do
21602197 "make sure to add parentheses after the function name)"
21612198
21622199 true ->
2163- message <> " in: #{ inspect ( term , pretty: true , limit: :infinity ) } "
2200+ Exception . _format_message_with_term (
2201+ message <> " in:" ,
2202+ term
2203+ )
21642204 end
21652205 end
21662206
@@ -2202,7 +2242,7 @@ defmodule KeyError do
22022242
22032243 case suggestions do
22042244 [ ] -> [ ]
2205- suggestions -> [ ". Did you mean:\n \n " | format_suggestions ( suggestions ) ]
2245+ suggestions -> [ "\n \n Did you mean:\n \n " | format_suggestions ( suggestions ) ]
22062246 end
22072247 end
22082248
0 commit comments