graphql-python / graphql-python/graphene-django
Raising exceptions without logging them
- Lingua principale
- Python
- Stelle
- 4.4k
- Fork
- 760
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Whenever exceptions are raised they're logged in the console (and in Sentry if it's used).
Many of these exceptions are only intended to be shown to the user. For example, [`django-graphql-jwt` raises the `PermissionDenied` exception](https://github.com/flavors/django-graphql-jwt/blob/master/graphql_jwt/exceptions.py#L14) for the [`login_required` decorator](https://github.com/flavors/django-graphql-jwt/blob/master/graphql_jwt/decorators.py#L48).
The problem is this pollutes the console output during testing/development and logs unnecessary errors to Sentry. For exceptions such as the example above, that's only intended to be shown to the user.
As a workaround I've tried writing middleware that catches any exceptions thrown:
```python
class ExceptionFilterMiddleware:
IGNORED_EXCEPTIONS = (
# Local exceptions
ValidationException,
# Third-party exceptions
JSONWebTokenExpired,
PermissionDenied,
)
def on_error(self, error):
if not isinstance(error, self.IGNORED_EXCEPTIONS):
return error
def resolve(self, next, *args, **kwargs):
return next(*args, **kwargs).catch(self.on_error)
```
But if an exception is caught, it no longer populates the `errors` field in query/mutation output. Therefore all errors are logged, there's no way to conditionally log exceptions.
This means the only solution is to create a logging filter like the following:
```python
def skip_valid_exceptions(record):
"""
Skip exceptions for errors only intended to be displayed to the API user.
"""
skip: bool = False
if record.exc_info:
exc_type, exc_value = record.exc_info[:2]
skip = isinstance(exc_value, valid_exceptions)
return not skip
```
But this doesn't work either because `record.exc_info` is `None` whenever an error is thrown with Graphene, therefore it's not possible to conditionally filter out exceptions based on their type.
Is there a solution for this? Seems like it'd be a common issue but I've had trouble finding one. [Something like this](https://medium.com/@tarkus/validation-and-user-errors-in-graphql-mutations-39ca79cd00bf) would be nice to be able to implement. Thanks in advance.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
L’issue non indica alcun file del repository né alcun test; inizia dal percorso del middleware `resolve`/`on_error` e dal comportamento di logging degli errori di Graphene descritto qui. Traccia come `PermissionDenied` raggiunge il campo `errors` della query o mutation e se `record.exc_info` viene preservato; il lavoro sarà completo quando esisterà un modo supportato per mantenere gli errori visibili all’utente sopprimendo condizionalmente i relativi log.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- django, graphql, python
- Ambito
- api
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100