Skip to content

Commit 9090df7

Browse files
committedFeb 23, 2024·
fix(account/middleware): Exception not handled
1 parent 079872a commit 9090df7

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed
 

‎allauth/account/middleware.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,25 @@ def AccountMiddleware(get_response):
1313

1414
async def middleware(request):
1515
with context.request_context(request):
16-
try:
17-
response = await get_response(request)
18-
if _should_check_dangling_login(request, response):
19-
await _acheck_dangling_login(request)
20-
return response
21-
except ImmediateHttpResponse as e:
22-
return e.response
16+
response = await get_response(request)
17+
if _should_check_dangling_login(request, response):
18+
await _acheck_dangling_login(request)
19+
return response
2320

2421
else:
2522

2623
def middleware(request):
2724
with context.request_context(request):
28-
try:
29-
response = get_response(request)
30-
if _should_check_dangling_login(request, response):
31-
_check_dangling_login(request)
32-
return response
33-
except ImmediateHttpResponse as e:
34-
return e.response
25+
response = get_response(request)
26+
if _should_check_dangling_login(request, response):
27+
_check_dangling_login(request)
28+
return response
3529

30+
def process_exception(request, exception):
31+
if isinstance(exception, ImmediateHttpResponse):
32+
return exception.response
33+
34+
middleware.process_exception = process_exception
3635
return middleware
3736

3837

‎allauth/account/tests/test_middleware.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from django.conf import settings
22
from django.http import HttpResponse
3+
from django.urls import path
34

45
import pytest
56

67
from allauth.account.middleware import AccountMiddleware
8+
from allauth.core.exceptions import ImmediateHttpResponse
79

810

911
@pytest.mark.parametrize(
@@ -35,3 +37,17 @@ def test_remove_dangling_login(
3537
mw = AccountMiddleware(lambda request: response)
3638
mw(request)
3739
assert ("account_login" in request.session) is (not login_removed)
40+
41+
42+
def raise_immediate_http_response(request):
43+
response = HttpResponse(content="raised-response")
44+
raise ImmediateHttpResponse(response=response)
45+
46+
47+
urlpatterns = [path("raise", raise_immediate_http_response)]
48+
49+
50+
def test_immediate_http_response(settings, client):
51+
settings.ROOT_URLCONF = "allauth.account.tests.test_middleware"
52+
resp = client.get("/raise")
53+
assert resp.content == b"raised-response"

0 commit comments

Comments
 (0)
Please sign in to comment.