graphql-core-legacy icon indicating copy to clipboard operation
graphql-core-legacy copied to clipboard

Add HandledGraphQLError that does not log an exception

Open Siecje opened this issue 7 years ago • 1 comments

I'm trying to send a custom error message in a web response. I can raise an exception to do that but I'm also getting sentry reports that I don't want.

In a Django project Sentry is setup to handle logging. When graphql-core logs this exception I get a sentry report.

https://github.com/graphql-python/graphql-core/blob/9202021fc87db9c175e115016cd53e5d9d085ac6/graphql/execution/executor.py#L446-L455

I want to be able to prevent the sentry report.

To send a custom error message back to the user you would just raise HandledGraphQLError(msg).

Here is the Django logging config for reference. Let me know if there is a better solution.

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'root': {
            'level': 'WARNING',
            'handlers': ['sentry'],
        },
        'formatters': {
            'verbose': {
                'format': '%(levelname)s %(asctime)s %(module)s '
                          '%(process)d %(thread)d %(message)s'
            },
        },
        'handlers': {
            'sentry': {
                # To capture more than ERROR, change to WARNING, INFO, etc.
                'level': 'ERROR',
                'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
                'tags': {
                    'build_number': os.environ.get('BUILD_NUMBER', 'none'),
                    'env': RUNNING_ENV
                }
            },
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose'
            }
        },
        'loggers': {
            'raven': {
                'level': 'DEBUG',
                'handlers': ['console'],
                'propagate': False,
            },
            'sentry.errors': {
                'level': 'DEBUG',
                'handlers': ['console'],
                'propagate': False,
            }
        },
    }

Siecje avatar Jan 31 '19 17:01 Siecje

There is another way! adding a filter to the django LOGGING setting and changing the level of the log record based on the raised exception.

LOGGING = {
'filters': {
            'graphql_log_filter': {
                '()': GraphQLLogFIlter
            }
        },
'graphql.execution.executor': {
                'level': 'INFO',
                'handlers': ['console'],
                'filters': ['graphql_log_filter']
            }
}

ziXet avatar Feb 04 '19 03:02 ziXet