Skip to content

Commit

Permalink
rely only got_request_exception mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobichaud committed Nov 27, 2024
1 parent 69b9496 commit 441c4ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
22 changes: 11 additions & 11 deletions django_structlog/middlewares/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions test_app/tests/middlewares/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 441c4ed

Please sign in to comment.