Skip to content

Commit ecdbf89

Browse files
Fix string serialization error raising (#1062)
* fix string serialization * rescue Protocol.UndefinedError * test name tweak Co-authored-by: Vince Foley <[email protected]>
1 parent 34cd95d commit ecdbf89

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/absinthe/phase/document/result.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ defmodule Absinthe.Phase.Document.Result do
5454
try do
5555
Type.Scalar.serialize(schema_node, value)
5656
rescue
57-
Absinthe.SerializationError ->
58-
raise(Absinthe.SerializationError, """
59-
Could not serialize term #{inspect(value)} as type #{schema_node.name}
60-
61-
When serializing the field:
62-
#{emitter.parent_type.name}.#{emitter.schema_node.name} (#{
63-
emitter.schema_node.__reference__.location.file
64-
}:#{emitter.schema_node.__reference__.location.line})
65-
""")
57+
_e in [Absinthe.SerializationError, Protocol.UndefinedError] ->
58+
raise(
59+
Absinthe.SerializationError,
60+
"""
61+
Could not serialize term #{inspect(value)} as type #{schema_node.name}
62+
63+
When serializing the field:
64+
#{emitter.parent_type.name}.#{emitter.schema_node.name} (#{
65+
emitter.schema_node.__reference__.location.file
66+
}:#{emitter.schema_node.__reference__.location.line})
67+
"""
68+
)
6669
end
6770

6871
%Type.Enum{} = schema_node ->

test/absinthe/integration/execution/serialization_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ defmodule Absinthe.Integration.Execution.SerializationTest do
1616
field :bad_boolean, :boolean do
1717
resolve fn _, _, _ -> {:ok, "true"} end
1818
end
19+
20+
field :bad_string, :string do
21+
resolve fn _, _, _ -> {:ok, %{}} end
22+
end
1923
end
2024
end
2125

@@ -45,4 +49,13 @@ defmodule Absinthe.Integration.Execution.SerializationTest do
4549
Absinthe.run(@query, Schema)
4650
end)
4751
end
52+
53+
@query """
54+
query { badString }
55+
"""
56+
test "returning a type that can't `to_string` for a string raises" do
57+
assert_raise(Absinthe.SerializationError, fn ->
58+
Absinthe.run(@query, Schema)
59+
end)
60+
end
4861
end

0 commit comments

Comments
 (0)