diff --git a/django_structlog/middlewares/request.py b/django_structlog/middlewares/request.py index ea9417b9..17826e01 100644 --- a/django_structlog/middlewares/request.py +++ b/django_structlog/middlewares/request.py @@ -116,16 +116,6 @@ def __call__( self.handle_response(request, response) return response - def process_got_request_exception( - self, sender: Type[Any], request: "HttpRequest", **kwargs: Any - ) -> None: - if not hasattr(request, "_raised_exception"): - ex = cast( - tuple[Type[Exception], Exception, "TracebackType"], - sys.exc_info(), - ) - self.process_exception(request, ex[1]) - async def __acall__(self, request: "HttpRequest") -> "HttpResponse": await sync.sync_to_async(self.prepare)(request) try: @@ -225,7 +215,17 @@ def bind_user_id(request: "HttpRequest") -> None: user_id = str(user_id) structlog.contextvars.bind_contextvars(user_id=user_id) - def process_exception(self, request: "HttpRequest", exception: Exception) -> None: + def process_got_request_exception( + self, sender: Type[Any], request: "HttpRequest", **kwargs: Any + ) -> None: + if not hasattr(request, "_raised_exception"): + ex = cast( + tuple[Type[Exception], Exception, "TracebackType"], + sys.exc_info(), + ) + self._process_exception(request, ex[1]) + + def _process_exception(self, request: "HttpRequest", exception: Exception) -> None: if isinstance(exception, (Http404, PermissionDenied)): # We don't log an exception here, and we don't set that we handled # an error as we want the standard `request_finished` log message diff --git a/test_app/tests/middlewares/test_request.py b/test_app/tests/middlewares/test_request.py index dc5913c7..2336a59f 100644 --- a/test_app/tests/middlewares/test_request.py +++ b/test_app/tests/middlewares/test_request.py @@ -11,6 +11,7 @@ from django.contrib.sites.models import Site from django.contrib.sites.shortcuts import get_current_site from django.core.exceptions import PermissionDenied +from django.core.signals import got_request_exception from django.dispatch import receiver from django.http import ( Http404, @@ -289,8 +290,8 @@ def get_response(_request: HttpRequest) -> HttpResponse: request.user = mock_user try: raise exception - except Exception as e: - middleware.process_exception(request, e) + except Exception: + got_request_exception.send(object, request=request) self.exception_traceback = traceback.format_exc() return mock_response @@ -463,7 +464,10 @@ def receiver_bind_extra_request_failed_metadata( request.user = mock_user def get_response(_request: HttpRequest) -> HttpResponse: - middleware.process_exception(request, expected_exception) + try: + raise expected_exception + except Exception: + got_request_exception.send(object, request=request) return mock_response middleware = RequestMiddleware(get_response) @@ -510,8 +514,8 @@ def get_response(_request: HttpRequest) -> Any: """Simulate an exception""" try: raise exception - except Exception as e: - middleware.process_exception(request, e) + except Exception: + got_request_exception.send(object, request=request) self.exception_traceback = traceback.format_exc() middleware = RequestMiddleware(get_response) @@ -563,8 +567,10 @@ def test_process_request_403_are_processed_as_regular_requests(self) -> None: request.user = AnonymousUser() def get_response(_request: HttpRequest) -> HttpResponse: - """Simulate an exception""" - middleware.process_exception(request, exception) + try: + raise exception + except Exception: + got_request_exception.send(object, request=request) return HttpResponseForbidden() middleware = RequestMiddleware(get_response) @@ -615,8 +621,10 @@ def test_process_request_404_are_processed_as_regular_requests(self) -> None: request.user = AnonymousUser() def get_response(_request: HttpRequest) -> HttpResponse: - """Simulate an exception""" - middleware.process_exception(request, exception) + try: + raise exception + except Exception: + got_request_exception.send(object, request=request) return HttpResponseNotFound() middleware = RequestMiddleware(get_response) @@ -668,8 +676,10 @@ def test_process_request_4XX_can_be_personalized(self) -> None: request.user = AnonymousUser() def get_response(_request: HttpRequest) -> HttpResponse: - """Simulate an exception""" - middleware.process_exception(request, exception) + try: + raise exception + except Exception: + got_request_exception.send(object, request=request) return HttpResponseNotFound() middleware = RequestMiddleware(get_response)