Skip to content

Commit a682f6f

Browse files
authored
Fix exception suppression in asgi transport (#2669)
1 parent f9abbbb commit a682f6f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
### Fixed
1010

11+
* Return `500` error response instead of exceptions when `raise_app_exceptions=False` is set on `ASGITransport`. (#2669)
1112
* Ensure all WSGITransport environs have a SERVER_PROTOCOL. (#2708)
1213

1314
## 0.24.1 (17th May, 2023)

httpx/_transports/asgi.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,15 @@ async def send(message: typing.Dict[str, typing.Any]) -> None:
161161
try:
162162
await self.app(scope, receive, send)
163163
except Exception: # noqa: PIE-786
164-
if self.raise_app_exceptions or not response_complete.is_set():
164+
if self.raise_app_exceptions:
165165
raise
166166

167+
response_complete.set()
168+
if status_code is None:
169+
status_code = 500
170+
if response_headers is None:
171+
response_headers = {}
172+
167173
assert response_complete.is_set()
168174
assert status_code is not None
169175
assert response_headers is not None

tests/test_asgi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import httpx
6+
from httpx import ASGITransport
67

78

89
async def hello_world(scope, receive, send):
@@ -191,3 +192,12 @@ async def read_body(scope, receive, send):
191192

192193
assert response.status_code == 200
193194
assert disconnect
195+
196+
197+
@pytest.mark.anyio
198+
async def test_asgi_exc_no_raise():
199+
transport = ASGITransport(app=raise_exc, raise_app_exceptions=False)
200+
async with httpx.AsyncClient(transport=transport) as client:
201+
response = await client.get("http://www.example.org/")
202+
203+
assert response.status_code == 500

0 commit comments

Comments
 (0)