Skip to content

Commit 1798df0

Browse files
committed
docs(django): clarify exception test limitations
Exception tests verify 500 responses but not actual PostHog capture. Exception capture requires process_exception() method from PR #350. Without process_exception(), Django converts view exceptions to responses before they propagate through the middleware context manager, so they're not captured to PostHog even though 500 is still returned.
1 parent 2521f4a commit 1798df0

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

test_project_django5/test_middleware.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,42 @@ async def test_async_exception_capture():
133133
"""
134134
Test that middleware captures exceptions from async views.
135135
136-
The middleware should capture the exception and send it to PostHog.
136+
IMPORTANT: This test verifies that exceptions raise 500 errors, but it doesn't
137+
verify that PostHog actually captures them. The exception capture fix (PR #350)
138+
adds a process_exception() method that Django calls to capture view exceptions.
139+
140+
Without process_exception(), Django converts view exceptions to responses
141+
before they propagate through the middleware context manager, so they're not
142+
captured to PostHog even though they still return 500 to the client.
143+
144+
To properly test exception capture, you'd need to mock posthog.capture_exception
145+
and verify it's called. That's tested in PR #350.
137146
"""
138147
app = get_asgi_application()
139148
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as ac:
140149
response = await ac.get("/test/async-exception")
141150

142151
# Django returns 500 for unhandled exceptions
143152
assert response.status_code == 500
144-
print("✓ Async exception capture test passed (exception raised as expected)")
153+
print("✓ Async exception raises 500 (capture requires process_exception from PR #350)")
145154

146155

147156
@pytest.mark.asyncio
148157
async def test_sync_exception_capture():
149158
"""
150-
Test that middleware captures exceptions from sync views.
159+
Test that middleware handles exceptions from sync views.
151160
152-
The middleware should capture the exception and send it to PostHog.
161+
IMPORTANT: Same as test_async_exception_capture - this verifies 500 response
162+
but not actual PostHog capture. Exception capture requires process_exception()
163+
method from PR #350.
153164
"""
154165
app = get_asgi_application()
155166
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as ac:
156167
response = await ac.get("/test/sync-exception")
157168

158169
# Django returns 500 for unhandled exceptions
159170
assert response.status_code == 500
160-
print("✓ Sync exception capture test passed (exception raised as expected)")
171+
print("✓ Sync exception raises 500 (capture requires process_exception from PR #350)")
161172

162173

163174
if __name__ == "__main__":

0 commit comments

Comments
 (0)