Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphene Django v3 swallowing all exceptions, including stack traces. #1437

Open
Toruitas opened this issue Jul 27, 2023 · 3 comments
Open
Labels

Comments

@Toruitas
Copy link

Toruitas commented Jul 27, 2023

  • 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

@EricRobertCampbell
Copy link

EricRobertCampbell commented Aug 9, 2023

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.

@lgnashold
Copy link

lgnashold commented Oct 18, 2023

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

@superlevure
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants