Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exception with cause exception #915

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ async def handle_async_request(self, request: Request) -> Response:
else:
break # pragma: nocover

except BaseException as exc:
except BaseException:
with self._optional_thread_lock:
# For any exception or cancellation we remove the request from
# the queue, and then re-assign requests to connections.
self._requests.remove(pool_request)
closing = self._assign_requests_to_connections()

await self._close_connections(closing)
raise exc from None
raise

# Return the response. Note that in this case we still have to manage
# the point at which the response is closed.
Expand Down Expand Up @@ -362,9 +362,9 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
try:
async for part in self._stream:
yield part
except BaseException as exc:
except BaseException:
await self.aclose()
raise exc from None
raise

async def aclose(self) -> None:
if not self._closed:
Expand Down
8 changes: 4 additions & 4 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ def handle_request(self, request: Request) -> Response:
else:
break # pragma: nocover

except BaseException as exc:
except BaseException:
with self._optional_thread_lock:
# For any exception or cancellation we remove the request from
# the queue, and then re-assign requests to connections.
self._requests.remove(pool_request)
closing = self._assign_requests_to_connections()

self._close_connections(closing)
raise exc from None
raise

# Return the response. Note that in this case we still have to manage
# the point at which the response is closed.
Expand Down Expand Up @@ -362,9 +362,9 @@ def __iter__(self) -> Iterator[bytes]:
try:
for part in self._stream:
yield part
except BaseException as exc:
except BaseException:
self.close()
raise exc from None
raise

def close(self) -> None:
if not self._closed:
Expand Down
8 changes: 6 additions & 2 deletions tests/_async/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ async def test_connection_pool_with_connect_exception():
be returned to the connection pool.
"""

cause_exception = Exception("cause")

class FailedConnectBackend(httpcore.AsyncMockBackend):
async def connect_tcp(
self,
Expand All @@ -421,7 +423,7 @@ async def connect_tcp(
typing.Iterable[httpcore.SOCKET_OPTION]
] = None,
) -> httpcore.AsyncNetworkStream:
raise httpcore.ConnectError("Could not connect")
raise httpcore.ConnectError("Could not connect") from cause_exception

network_backend = FailedConnectBackend([])

Expand All @@ -432,11 +434,13 @@ async def trace(name, kwargs):

async with httpcore.AsyncConnectionPool(network_backend=network_backend) as pool:
# Sending an initial request, which once complete will not return to the pool.
with pytest.raises(Exception):
with pytest.raises(Exception) as exc_info:
await pool.request(
"GET", "https://example.com/", extensions={"trace": trace}
)

assert exc_info.value.__cause__ is cause_exception

info = [repr(c) for c in pool.connections]
assert info == []

Expand Down
8 changes: 6 additions & 2 deletions tests/_sync/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ def test_connection_pool_with_connect_exception():
be returned to the connection pool.
"""

cause_exception = Exception("cause")

class FailedConnectBackend(httpcore.MockBackend):
def connect_tcp(
self,
Expand All @@ -421,7 +423,7 @@ def connect_tcp(
typing.Iterable[httpcore.SOCKET_OPTION]
] = None,
) -> httpcore.NetworkStream:
raise httpcore.ConnectError("Could not connect")
raise httpcore.ConnectError("Could not connect") from cause_exception

network_backend = FailedConnectBackend([])

Expand All @@ -432,11 +434,13 @@ def trace(name, kwargs):

with httpcore.ConnectionPool(network_backend=network_backend) as pool:
# Sending an initial request, which once complete will not return to the pool.
with pytest.raises(Exception):
with pytest.raises(Exception) as exc_info:
pool.request(
"GET", "https://example.com/", extensions={"trace": trace}
)

assert exc_info.value.__cause__ is cause_exception

info = [repr(c) for c in pool.connections]
assert info == []

Expand Down
Loading