Skip to content

Commit e3778a4

Browse files
better name
1 parent 4254af5 commit e3778a4

File tree

8 files changed

+29
-21
lines changed

8 files changed

+29
-21
lines changed

httpcore/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ReadError,
3030
ReadTimeout,
3131
RemoteProtocolError,
32+
ServerDisconnectedError,
3233
TimeoutException,
3334
UnsupportedProtocol,
3435
WriteError,
@@ -114,6 +115,7 @@ def __init__(self, *args, **kwargs): # type: ignore
114115
"SOCKET_OPTION",
115116
# exceptions
116117
"ConnectionNotAvailable",
118+
"ServerDisconnectedError",
117119
"ProxyError",
118120
"ProtocolError",
119121
"LocalProtocolError",

httpcore/_async/connection_pool.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
88
from .._exceptions import (
99
ConnectionNotAvailable,
10-
ServerDisconnectedInternalError,
10+
ServerDisconnectedError,
1111
UnsupportedProtocol,
1212
)
1313
from .._models import Origin, Request, Response
@@ -200,7 +200,7 @@ async def handle_async_request(self, request: Request) -> Response:
200200
response = await connection.handle_async_request(
201201
pool_request.request
202202
)
203-
except (ConnectionNotAvailable, ServerDisconnectedInternalError):
203+
except (ConnectionNotAvailable, ServerDisconnectedError):
204204
# In some cases a connection may initially be available to
205205
# handle a request, but then become unavailable.
206206
#

httpcore/_async/http11.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
ConnectionNotAvailable,
2222
LocalProtocolError,
2323
RemoteProtocolError,
24-
ServerDisconnectedInternalError,
24+
ServerDisconnectedError,
2525
WriteError,
2626
map_exceptions,
2727
)
@@ -79,19 +79,19 @@ async def handle_async_request(self, request: Request) -> Response:
7979
)
8080

8181
async with self._state_lock:
82+
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
83+
raise ServerDisconnectedError()
84+
8285
# If the HTTP connection is idle but the socket is readable, then the
8386
# only valid state is that the socket is about to return b"", indicating
8487
# a server-initiated disconnect.
8588
server_disconnected = (
8689
self._state == HTTPConnectionState.IDLE
8790
and self._network_stream.get_extra_info("is_readable")
8891
)
89-
if (
90-
server_disconnected
91-
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
92-
):
92+
if server_disconnected:
9393
self._state = HTTPConnectionState.SERVER_DISCONNECTED
94-
raise ServerDisconnectedInternalError()
94+
raise ServerDisconnectedError()
9595

9696
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
9797
self._request_count += 1

httpcore/_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ConnectionNotAvailable(Exception):
1919
pass
2020

2121

22-
class ServerDisconnectedInternalError(Exception):
22+
class ServerDisconnectedError(Exception):
2323
pass
2424

2525

httpcore/_sync/connection_pool.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .._backends.base import SOCKET_OPTION, NetworkBackend
88
from .._exceptions import (
99
ConnectionNotAvailable,
10-
ServerDisconnectedInternalError,
10+
ServerDisconnectedError,
1111
UnsupportedProtocol,
1212
)
1313
from .._models import Origin, Request, Response
@@ -200,7 +200,7 @@ def handle_request(self, request: Request) -> Response:
200200
response = connection.handle_request(
201201
pool_request.request
202202
)
203-
except (ConnectionNotAvailable, ServerDisconnectedInternalError):
203+
except (ConnectionNotAvailable, ServerDisconnectedError):
204204
# In some cases a connection may initially be available to
205205
# handle a request, but then become unavailable.
206206
#

httpcore/_sync/http11.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
ConnectionNotAvailable,
2222
LocalProtocolError,
2323
RemoteProtocolError,
24-
ServerDisconnectedInternalError,
24+
ServerDisconnectedError,
2525
WriteError,
2626
map_exceptions,
2727
)
@@ -79,19 +79,19 @@ def handle_request(self, request: Request) -> Response:
7979
)
8080

8181
with self._state_lock:
82+
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
83+
raise ServerDisconnectedError()
84+
8285
# If the HTTP connection is idle but the socket is readable, then the
8386
# only valid state is that the socket is about to return b"", indicating
8487
# a server-initiated disconnect.
8588
server_disconnected = (
8689
self._state == HTTPConnectionState.IDLE
8790
and self._network_stream.get_extra_info("is_readable")
8891
)
89-
if (
90-
server_disconnected
91-
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
92-
):
92+
if server_disconnected:
9393
self._state = HTTPConnectionState.SERVER_DISCONNECTED
94-
raise ServerDisconnectedInternalError()
94+
raise ServerDisconnectedError()
9595

9696
if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
9797
self._request_count += 1

tests/_async/test_http11.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
import httpcore
6-
from httpcore._exceptions import ServerDisconnectedInternalError
6+
from httpcore._exceptions import ServerDisconnectedError
77

88

99
@pytest.mark.anyio
@@ -202,9 +202,12 @@ def get_extra_info(self, info: str) -> typing.Any:
202202
assert conn.is_idle() and not conn.has_expired()
203203
stream.mock_is_readable = True # Simulate connection breakage
204204

205-
with pytest.raises(ServerDisconnectedInternalError):
205+
with pytest.raises(ServerDisconnectedError):
206206
await conn.request("GET", "https://example.com/")
207+
assert conn.has_expired() and not conn.is_idle()
207208

209+
with pytest.raises(ServerDisconnectedError):
210+
await conn.request("GET", "https://example.com/")
208211
assert conn.has_expired() and not conn.is_idle()
209212

210213

tests/_sync/test_http11.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
import httpcore
6-
from httpcore._exceptions import ServerDisconnectedInternalError
6+
from httpcore._exceptions import ServerDisconnectedError
77

88

99

@@ -202,9 +202,12 @@ def get_extra_info(self, info: str) -> typing.Any:
202202
assert conn.is_idle() and not conn.has_expired()
203203
stream.mock_is_readable = True # Simulate connection breakage
204204

205-
with pytest.raises(ServerDisconnectedInternalError):
205+
with pytest.raises(ServerDisconnectedError):
206206
conn.request("GET", "https://example.com/")
207+
assert conn.has_expired() and not conn.is_idle()
207208

209+
with pytest.raises(ServerDisconnectedError):
210+
conn.request("GET", "https://example.com/")
208211
assert conn.has_expired() and not conn.is_idle()
209212

210213

0 commit comments

Comments
 (0)