From fd567c7d1f3127e40905ab5bc5ff4f15f571c759 Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Tue, 19 Jan 2021 22:28:51 -0500 Subject: [PATCH] Improved serialization failure messages (#1033) * improved error messages on serialization failure * tweak wording * changelog entry --- CHANGELOG.md | 4 ++++ lib/absinthe/phase/document/result.ex | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b71f5a7e7c..acfa8fc4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.6.1 + +- Feature: [Improved serialization failure messages](https://github.com/absinthe-graphql/absinthe/pull/1033) + ## 1.6.0 - Feature: [Interfaces can now implement Interfaces](https://github.com/absinthe-graphql/absinthe/pull/1012), matching the latest spec diff --git a/lib/absinthe/phase/document/result.ex b/lib/absinthe/phase/document/result.ex index ec6a776e9d..6fe940b393 100644 --- a/lib/absinthe/phase/document/result.ex +++ b/lib/absinthe/phase/document/result.ex @@ -51,7 +51,19 @@ defmodule Absinthe.Phase.Document.Result do value = case Type.unwrap(emitter.schema_node.type) do %Type.Scalar{} = schema_node -> - Type.Scalar.serialize(schema_node, value) + try do + Type.Scalar.serialize(schema_node, value) + rescue + Absinthe.SerializationError -> + raise(Absinthe.SerializationError, """ + Could not serialize term #{inspect(value)} as type #{schema_node.name} + + When serializing the field: + #{emitter.parent_type.name}.#{emitter.schema_node.name} (#{ + emitter.schema_node.__reference__.location.file + }:#{emitter.schema_node.__reference__.location.line}) + """) + end %Type.Enum{} = schema_node -> Type.Enum.serialize(schema_node, value)