graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.

Home Page:http://docs.graphene-python.org/projects/django/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graphene Django v3 swallowing all exceptions, including stack traces.

Toruitas opened this issue · comments

  • What is the current behavior?
    In v3, both intentionally raised exceptions and unexpected exceptions are swallowed entirely without any indication in the console. They only return any information at all in the response, which of course doesn't include the stack trace. Even in Debug mode.

  • Code example
    Based on one of the graphene-django examples, set up as normal and try the 3 queries in the readme:
    https://github.com/Toruitas/Graphene-Django-BR

  • What is the expected behavior?
    Exceptions should be logged as normal to the console, with a stack trace. Except perhaps explicit GraphQLError exceptions.

  • What is the motivation / use case for changing the behavior?
    Logging exceptions with stack traces to find and fix errors is absolutely critical for hopefully obvious reasons.

  • Please tell us about your environment:

Django 3.2.20
Graphene-Django 3.1.3
Graphene 3.3

For comparison (this version set produces exceptions in the console)
Django 3.2.20
Graphene-Django 2.16.0
Graphene 2.1.9

Logging and visibility for stack traces is necessary for both development and production code in order to answer simple questions like "Where is the code that broke?" I'm migrating a project from Graphene-Django v2 to v3 and am encountering an issue regarding exception swallowing. In v2, both explicit Exceptions and unexpected Exceptions will log to the console with stack traces and also return the error message in the GraphQL response. In v3, the same exact code with no changes at all besides the version upgrade won't log anything to the console, but will return the error message in the GraphQL response. I would expect to get the exception and stack trace in the console as well.

Or is there some new option to log exceptions that is now disabled by default that I might have missed?

Thanks!

Edit:
Bug still present on Graphene-Django 3.2.0

I was running into the same issue. There's a partial workaround (https://stackoverflow.com/questions/75166816/django-graphene-not-handling-errors/75179165#75179165) which returns the error attribute as part of the response but doesn't log it to the console.

Looks like it might also be related to #1384.

Also having this issue. Was not able to get the workaround above to work.

You can use a middleware to achieve this:

import logging

logger = logging.getLogger(__name__)


class ExceptionLoggingMiddleware:
    def resolve(self, next, root, info, **args):
        try:
            return next(root, info, **args)
        except Exception as e:
            raise self._log_exception(e)

    def _log_exception(self, error: Exception) -> Exception:
        logger.exception("Exception caught in resolver.", exc_info=error)

        return error