From cbdc7e9786dd9eb9551f99fcbb07b3b6813688c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Rivi=C3=A8re?= Date: Sun, 20 Mar 2022 22:48:54 +0100 Subject: [PATCH] feat: log exception by default --- graphene_django/views.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/graphene_django/views.py b/graphene_django/views.py index bf333a9..84302ab 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -1,6 +1,7 @@ import inspect import json import re +from traceback import print_tb from django.db import connection, transaction from django.http import HttpResponse, HttpResponseNotAllowed @@ -53,6 +54,20 @@ def instantiate_middleware(middlewares): yield middleware +class ExceptionLoggingExecutionContext(ExecutionContext): + + """An execution context which logs exceptions.""" + + def handle_field_error( + self, + error: GraphQLError, + return_type, + ) -> None: + + print_tb(error.original_error.__traceback__) + return super().handle_field_error(error, return_type) + + class GraphQLView(View): graphiql_template = "graphene/graphiql.html" @@ -94,7 +109,7 @@ class GraphQLView(View): pretty=False, batch=False, subscription_path=None, - execution_context_class=None, + execution_context_class=ExceptionLoggingExecutionContext, ): if not schema: schema = graphene_settings.SCHEMA