Skip to content

Commit 2f844fd

Browse files
authored
Move value in protocol errors to its own line (#14003)
1 parent 50619b1 commit 2f844fd

File tree

5 files changed

+73
-21
lines changed

5 files changed

+73
-21
lines changed

lib/elixir/lib/exception.ex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,8 +1997,23 @@ defmodule Protocol.UndefinedError do
19971997

19981998
@impl true
19991999
def message(%{protocol: protocol, value: value, description: description}) do
2000-
"protocol #{inspect(protocol)} not implemented for #{inspect(value)} of type " <>
2001-
value_type(value) <> maybe_description(description) <> maybe_available(protocol)
2000+
inspected =
2001+
value
2002+
|> inspect(pretty: true)
2003+
# Indent only lines with contents on them
2004+
|> String.replace(~r/^(?=.+)/m, " ")
2005+
2006+
"protocol #{inspect(protocol)} not implemented for type " <>
2007+
value_type(value) <>
2008+
maybe_description(description) <>
2009+
maybe_available(protocol) <>
2010+
"""
2011+
2012+
2013+
Got value:
2014+
2015+
#{inspected}
2016+
"""
20022017
end
20032018

20042019
defp value_type(%{__struct__: struct}), do: "#{inspect(struct)} (a struct)"

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,20 @@ defmodule Inspect.MapTest do
526526
# Inspect.Error is raised here when we tried to print the error message
527527
# called by another exception (Protocol.UndefinedError in this case)
528528
exception_message = ~s'''
529-
protocol Enumerable not implemented for #Inspect.Error<
530-
got ArgumentError with message:
529+
protocol Enumerable not implemented for type Inspect.MapTest.Failing (a struct)
531530
532-
"""
533-
errors were found at the given arguments:
531+
Got value:
534532
535-
* 1st argument: not an atom
536-
"""
533+
#Inspect.Error<
534+
got ArgumentError with message:
537535
538-
while inspecting:
536+
"""
537+
errors were found at the given arguments:
538+
539+
* 1st argument: not an atom
540+
"""
541+
542+
while inspecting:
539543
540544
'''
541545

@@ -926,7 +930,11 @@ defmodule Inspect.CustomProtocolTest do
926930
got Protocol.UndefinedError with message:
927931
928932
"""
929-
protocol Inspect.CustomProtocolTest.CustomInspect not implemented for %Inspect.CustomProtocolTest.MissingImplementation{} of type Inspect.CustomProtocolTest.MissingImplementation (a struct)
933+
protocol Inspect.CustomProtocolTest.CustomInspect not implemented for type Inspect.CustomProtocolTest.MissingImplementation (a struct)
934+
935+
Got value:
936+
937+
%Inspect.CustomProtocolTest.MissingImplementation{}
930938
"""
931939
932940
while inspecting:
@@ -953,7 +961,11 @@ defmodule Inspect.CustomProtocolTest do
953961
got Protocol.UndefinedError with message:
954962
955963
"""
956-
protocol Inspect.CustomProtocolTest.CustomInspect not implemented for %Inspect.CustomProtocolTest.MissingImplementation{} of type Inspect.CustomProtocolTest.MissingImplementation (a struct)
964+
protocol Inspect.CustomProtocolTest.CustomInspect not implemented for type Inspect.CustomProtocolTest.MissingImplementation (a struct)
965+
966+
Got value:
967+
968+
%Inspect.CustomProtocolTest.MissingImplementation{}
957969
"""
958970
959971
while inspecting:

lib/elixir/test/elixir/protocol/consolidation_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ defmodule Protocol.ConsolidationTest do
182182

183183
test "protocol not implemented" do
184184
message =
185-
"protocol Protocol.ConsolidationTest.Sample not implemented for :foo of type Atom. " <>
186-
"This protocol is implemented for the following type(s): Protocol.ConsolidationTest.ImplStruct"
185+
"protocol Protocol.ConsolidationTest.Sample not implemented for type Atom. " <>
186+
"This protocol is implemented for the following type(s): Protocol.ConsolidationTest.ImplStruct" <>
187+
"\n\nGot value:\n\n :foo\n"
187188

188189
assert_raise Protocol.UndefinedError, message, fn ->
189190
sample = String.to_atom("Elixir.Protocol.ConsolidationTest.Sample")

lib/elixir/test/elixir/protocol_test.exs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,14 @@ defmodule ProtocolTest do
113113
end
114114

115115
test "protocol not implemented" do
116-
message = "protocol ProtocolTest.Sample not implemented for :foo of type Atom"
116+
message =
117+
"""
118+
protocol ProtocolTest.Sample not implemented for type Atom
119+
120+
Got value:
121+
122+
:foo
123+
"""
117124

118125
assert_raise Protocol.UndefinedError, message, fn ->
119126
sample = String.to_atom("Elixir.ProtocolTest.Sample")

lib/elixir/test/elixir/string/chars_test.exs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,39 +105,54 @@ defmodule String.Chars.ErrorsTest do
105105

106106
test "bitstring" do
107107
message =
108-
"protocol String.Chars not implemented for <<0, 1::size(4)>> of type BitString, cannot convert a bitstring to a string"
108+
"""
109+
protocol String.Chars not implemented for type BitString, cannot convert a bitstring to a string
110+
111+
Got value:
112+
113+
<<0, 1::size(4)>>
114+
"""
109115

110116
assert_raise Protocol.UndefinedError, message, fn ->
111117
to_string(<<1::size(12)-integer-signed>>)
112118
end
113119
end
114120

115121
test "tuple" do
116-
message = "protocol String.Chars not implemented for {1, 2, 3} of type Tuple"
122+
message = """
123+
protocol String.Chars not implemented for type Tuple
124+
125+
Got value:
126+
127+
{1, 2, 3}
128+
"""
117129

118130
assert_raise Protocol.UndefinedError, message, fn ->
119131
to_string({1, 2, 3})
120132
end
121133
end
122134

123135
test "PID" do
124-
message = ~r"^protocol String\.Chars not implemented for #PID<.+?> of type PID$"
136+
message =
137+
~r"^protocol String\.Chars not implemented for type PID\n\nGot value:\n\n #PID<.+?>$"
125138

126139
assert_raise Protocol.UndefinedError, message, fn ->
127140
to_string(self())
128141
end
129142
end
130143

131144
test "ref" do
132-
message = ~r"^protocol String\.Chars not implemented for #Reference<.+?> of type Reference$"
145+
message =
146+
~r"^protocol String\.Chars not implemented for type Reference\n\nGot value:\n\n #Reference<.+?>$"
133147

134148
assert_raise Protocol.UndefinedError, message, fn ->
135149
to_string(make_ref()) == ""
136150
end
137151
end
138152

139153
test "function" do
140-
message = ~r"^protocol String\.Chars not implemented for #Function<.+?> of type Function$"
154+
message =
155+
~r"^protocol String\.Chars not implemented for type Function\n\nGot value:\n\n #Function<.+?>$"
141156

142157
assert_raise Protocol.UndefinedError, message, fn ->
143158
to_string(fn -> nil end)
@@ -146,7 +161,9 @@ defmodule String.Chars.ErrorsTest do
146161

147162
test "port" do
148163
[port | _] = Port.list()
149-
message = ~r"^protocol String\.Chars not implemented for #Port<.+?> of type Port$"
164+
165+
message =
166+
~r"^protocol String\.Chars not implemented for type Port\n\nGot value:\n\n #Port<.+?>$"
150167

151168
assert_raise Protocol.UndefinedError, message, fn ->
152169
to_string(port)
@@ -155,7 +172,7 @@ defmodule String.Chars.ErrorsTest do
155172

156173
test "user-defined struct" do
157174
message =
158-
"protocol String\.Chars not implemented for %String.Chars.ErrorsTest.Foo{foo: \"bar\"} of type String.Chars.ErrorsTest.Foo (a struct)"
175+
"protocol String\.Chars not implemented for type String.Chars.ErrorsTest.Foo (a struct)\n\nGot value:\n\n %String.Chars.ErrorsTest.Foo{foo: \"bar\"}\n"
159176

160177
assert_raise Protocol.UndefinedError, message, fn ->
161178
to_string(%Foo{})

0 commit comments

Comments
 (0)