Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/custom-errors.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Custom errors
=============

Default GraphQL error format similar to the following snippet

.. code:: json

{
"errors": [
{
"message": "Variable \"$myAwesomeField\" of required type \"String!\" was not provided.",
"locations": [
{
"line": 1,
"column": 13
}
]
}
]
}

And there is a way customise it by swapping default ``GraphQLView`` with your own
and then override ``format_error`` method

.. code:: python

class MyGraphQLView(GraphQLView):
@staticmethod
def format_error(error) -> Dict[str, Any]:
if isinstance(error, GraphQLError):
return format_error(error)

return GraphQLView.format_error(error)


Here is custom formatting function

.. code:: python

def format_error(error: GraphQLError) -> Dict[str, Any]:
"""Extract field from ``error`` and return formatted error
:param error: GraphQLError
:return: mapping of fieldName -> error message
"""
formatted_error = {
n.variable.name.value: str(error)
for n in error.nodes
}

if error.path:
formatted_error["path"] = error.path

return formatted_error


.. note::
``error.nodes`` might be other GraphQL type as well.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Contents:
debug
rest-framework
form-mutations
introspection
custom-errors