From 08a42961da26adc453d83b0c689e4d268ba3994d Mon Sep 17 00:00:00 2001 From: Sultan Iman Date: Thu, 1 Nov 2018 22:25:37 +0100 Subject: [PATCH 1/3] docs: document how custom error formatting works --- docs/custom-errors.rst | 51 ++++++++++++++++++++++++++++++++++++++++++ docs/index.rst | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 docs/custom-errors.rst diff --git a/docs/custom-errors.rst b/docs/custom-errors.rst new file mode 100644 index 000000000..f485d60ac --- /dev/null +++ b/docs/custom-errors.rst @@ -0,0 +1,51 @@ +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 diff --git a/docs/index.rst b/docs/index.rst index 7c64ae709..061cb9064 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -13,4 +13,4 @@ Contents: debug rest-framework form-mutations - introspection + custom-errors From 18872ed8d5471abac51c6baa9e7ef6c499cd87de Mon Sep 17 00:00:00 2001 From: Sultan Iman Date: Thu, 1 Nov 2018 22:29:29 +0100 Subject: [PATCH 2/3] fix: code blocks missing colon --- docs/custom-errors.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/custom-errors.rst b/docs/custom-errors.rst index f485d60ac..b86e2dcca 100644 --- a/docs/custom-errors.rst +++ b/docs/custom-errors.rst @@ -3,7 +3,7 @@ Custom errors Default GraphQL error format similar to the following snippet -.. code: json +.. code:: json { "errors": [ @@ -22,7 +22,8 @@ Default GraphQL error format similar to the following snippet And there is a way customise it by swapping default ``GraphQLView`` with your own and then override ``format_error`` method -.. code: python +.. code:: python + class MyGraphQLView(GraphQLView): @staticmethod def format_error(error) -> Dict[str, Any]: @@ -34,7 +35,8 @@ and then override ``format_error`` method Here is custom formatting function -.. code: python +.. code:: python + def format_error(error: GraphQLError) -> Dict[str, Any]: """Extract field from ``error`` and return formatted error :param error: GraphQLError From cb8f8992924bef2ebe642bd86c0020c7ba107b91 Mon Sep 17 00:00:00 2001 From: Sultan Iman Date: Thu, 1 Nov 2018 22:32:15 +0100 Subject: [PATCH 3/3] docs: add note on node.errors --- docs/custom-errors.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/custom-errors.rst b/docs/custom-errors.rst index b86e2dcca..9d670bbe3 100644 --- a/docs/custom-errors.rst +++ b/docs/custom-errors.rst @@ -51,3 +51,7 @@ Here is custom formatting function formatted_error["path"] = error.path return formatted_error + + +.. note:: + ``error.nodes`` might be other GraphQL type as well.