88With process_exception(), Django calls this method to capture exceptions before
99converting them to 500 responses.
1010"""
11+
1112import os
1213import django
1314
1415# Setup Django before importing anything else
1516os .environ .setdefault ("DJANGO_SETTINGS_MODULE" , "testdjango.settings" )
1617django .setup ()
1718
18- import pytest
19- from httpx import AsyncClient , ASGITransport
20- from django .core .asgi import get_asgi_application
19+ import pytest # noqa: E402
20+ from httpx import AsyncClient , ASGITransport # noqa: E402
21+ from django .core .asgi import get_asgi_application # noqa: E402
2122
2223
2324@pytest .fixture (scope = "session" )
@@ -41,27 +42,31 @@ async def test_async_exception_is_captured(asgi_app):
4142
4243 def mock_capture (exception , ** kwargs ):
4344 """Mock capture_exception to record calls."""
44- captured .append ({
45- 'exception' : exception ,
46- 'type' : type (exception ).__name__ ,
47- 'message' : str (exception )
48- })
45+ captured .append (
46+ {
47+ "exception" : exception ,
48+ "type" : type (exception ).__name__ ,
49+ "message" : str (exception ),
50+ }
51+ )
4952
5053 # Patch at the posthog module level where middleware imports from
51- with patch ('posthog.capture_exception' , side_effect = mock_capture ):
52- async with AsyncClient (transport = ASGITransport (app = asgi_app ), base_url = "http://testserver" ) as ac :
54+ with patch ("posthog.capture_exception" , side_effect = mock_capture ):
55+ async with AsyncClient (
56+ transport = ASGITransport (app = asgi_app ), base_url = "http://testserver"
57+ ) as ac :
5358 response = await ac .get ("/test/async-exception" )
5459
5560 # Django returns 500
5661 assert response .status_code == 500
5762
5863 # CRITICAL: Verify PostHog captured the exception
59- assert len (captured ) > 0 , f "Exception was NOT captured to PostHog!"
64+ assert len (captured ) > 0 , "Exception was NOT captured to PostHog!"
6065
6166 # Verify it's the right exception
6267 exception_data = captured [0 ]
63- assert exception_data [' type' ] == ' ValueError'
64- assert ' Test exception from Django 5 async view' in exception_data [' message' ]
68+ assert exception_data [" type" ] == " ValueError"
69+ assert " Test exception from Django 5 async view" in exception_data [" message" ]
6570
6671
6772@pytest .mark .asyncio
@@ -79,24 +84,28 @@ async def test_sync_exception_is_captured(asgi_app):
7984
8085 def mock_capture (exception , ** kwargs ):
8186 """Mock capture_exception to record calls."""
82- captured .append ({
83- 'exception' : exception ,
84- 'type' : type (exception ).__name__ ,
85- 'message' : str (exception )
86- })
87+ captured .append (
88+ {
89+ "exception" : exception ,
90+ "type" : type (exception ).__name__ ,
91+ "message" : str (exception ),
92+ }
93+ )
8794
8895 # Patch at the posthog module level where middleware imports from
89- with patch ('posthog.capture_exception' , side_effect = mock_capture ):
90- async with AsyncClient (transport = ASGITransport (app = asgi_app ), base_url = "http://testserver" ) as ac :
96+ with patch ("posthog.capture_exception" , side_effect = mock_capture ):
97+ async with AsyncClient (
98+ transport = ASGITransport (app = asgi_app ), base_url = "http://testserver"
99+ ) as ac :
91100 response = await ac .get ("/test/sync-exception" )
92101
93102 # Django returns 500
94103 assert response .status_code == 500
95104
96105 # CRITICAL: Verify PostHog captured the exception
97- assert len (captured ) > 0 , f "Exception was NOT captured to PostHog!"
106+ assert len (captured ) > 0 , "Exception was NOT captured to PostHog!"
98107
99108 # Verify it's the right exception
100109 exception_data = captured [0 ]
101- assert exception_data [' type' ] == ' ValueError'
102- assert ' Test exception from Django 5 sync view' in exception_data [' message' ]
110+ assert exception_data [" type" ] == " ValueError"
111+ assert " Test exception from Django 5 sync view" in exception_data [" message" ]
0 commit comments