Features: 1) Introduced distinct warning and error logging for specific exceptions in GraphQL middleware; 2) Added new exception types for enhanced validation handling.

Fixes: 1) Corrected indentation inconsistency in delete_never_ordered_products command; 2) Fixed improper formatting of log handler configurations.

Extra: Adjusted logging to conform to best practices; amended middleware exception handling logic to improve code readability.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-07 19:12:43 +03:00
parent 4ef06dd971
commit a562591900
3 changed files with 22 additions and 16 deletions

View file

@ -18,9 +18,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
size = options["size"]
while True:
batch_ids = list(
Product.objects.filter(orderproduct__isnull=True).values_list("pk", flat=True)[:size]
)
batch_ids = list(Product.objects.filter(orderproduct__isnull=True).values_list("pk", flat=True)[:size])
if not batch_ids:
break
try:

View file

@ -4,7 +4,7 @@ from os import getenv
from constance import config
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import BadRequest, DisallowedHost
from django.core.exceptions import BadRequest, DisallowedHost, PermissionDenied, ValidationError
from django.http import HttpResponseForbidden
from django.middleware.common import CommonMiddleware
from django.middleware.locale import LocaleMiddleware
@ -95,13 +95,21 @@ class BlockInvalidHostMiddleware:
# noinspection PyShadowingBuiltins
class GrapheneLoggingErrorsDebugMiddleware:
WARNING_ONLY_ERRORS = [
BadRequest,
PermissionDenied,
DisallowedHost,
ValidationError,
]
def resolve(self, next, root, info, **args):
try:
return next(root, info, **args)
except Exception as e:
logger.error("Error occurred in GraphQL execution:", exc_info=True)
if bool(int(getenv("DEBUG"))):
if e in self.WARNING_ONLY_ERRORS:
logger.warning(str(e))
else:
logger.error(str(e))
logger.error(traceback.format_exc())
capture_exception(e)
capture_exception(e)
raise e

View file

@ -66,7 +66,7 @@ LOGGING = {
"handlers": [
"console",
"mail_admins",
],
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": True,
},
@ -78,7 +78,7 @@ LOGGING = {
"handlers": [
"console",
"mail_admins",
],
],
"level": "WARNING",
"propagate": False,
},
@ -86,7 +86,7 @@ LOGGING = {
"handlers": [
"console",
"mail_admins",
],
],
"level": "DEBUG" if DEBUG else "ERROR",
"propagate": True,
"filters": ["skip_variable_doesnotexist"],
@ -94,42 +94,42 @@ LOGGING = {
"gunicorn.access": {
"handlers": [
"console",
],
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"gunicorn.error": {
"handlers": [
"console",
],
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"django_elasticsearch_dsl": {
"handlers": [
"console",
],
],
"level": "WARNING",
"propagate": False,
},
"celery.app.trace": {
"handlers": [
"console",
],
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"celery.worker.strategy": {
"handlers": [
"console",
],
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"elastic_transport.transport": {
"handlers": [
"console",
],
],
"level": "ERROR",
"propagate": False,
},