Skip to content

Commit

Permalink
better name
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 15, 2024
1 parent 4254af5 commit e3778a4
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 21 deletions.
2 changes: 2 additions & 0 deletions httpcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ReadError,
ReadTimeout,
RemoteProtocolError,
ServerDisconnectedError,
TimeoutException,
UnsupportedProtocol,
WriteError,
Expand Down Expand Up @@ -114,6 +115,7 @@ def __init__(self, *args, **kwargs): # type: ignore
"SOCKET_OPTION",
# exceptions
"ConnectionNotAvailable",
"ServerDisconnectedError",
"ProxyError",
"ProtocolError",
"LocalProtocolError",
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
from .._exceptions import (
ConnectionNotAvailable,
ServerDisconnectedInternalError,
ServerDisconnectedError,
UnsupportedProtocol,
)
from .._models import Origin, Request, Response
Expand Down Expand Up @@ -200,7 +200,7 @@ async def handle_async_request(self, request: Request) -> Response:
response = await connection.handle_async_request(
pool_request.request
)
except (ConnectionNotAvailable, ServerDisconnectedInternalError):
except (ConnectionNotAvailable, ServerDisconnectedError):
# In some cases a connection may initially be available to
# handle a request, but then become unavailable.
#
Expand Down
12 changes: 6 additions & 6 deletions httpcore/_async/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ConnectionNotAvailable,
LocalProtocolError,
RemoteProtocolError,
ServerDisconnectedInternalError,
ServerDisconnectedError,
WriteError,
map_exceptions,
)
Expand Down Expand Up @@ -79,19 +79,19 @@ async def handle_async_request(self, request: Request) -> Response:
)

async with self._state_lock:
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
raise ServerDisconnectedError()

# If the HTTP connection is idle but the socket is readable, then the
# only valid state is that the socket is about to return b"", indicating
# a server-initiated disconnect.
server_disconnected = (
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
if (
server_disconnected
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
):
if server_disconnected:
self._state = HTTPConnectionState.SERVER_DISCONNECTED
raise ServerDisconnectedInternalError()
raise ServerDisconnectedError()

if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
self._request_count += 1
Expand Down
2 changes: 1 addition & 1 deletion httpcore/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ConnectionNotAvailable(Exception):
pass


class ServerDisconnectedInternalError(Exception):
class ServerDisconnectedError(Exception):
pass


Expand Down
4 changes: 2 additions & 2 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .._backends.base import SOCKET_OPTION, NetworkBackend
from .._exceptions import (
ConnectionNotAvailable,
ServerDisconnectedInternalError,
ServerDisconnectedError,
UnsupportedProtocol,
)
from .._models import Origin, Request, Response
Expand Down Expand Up @@ -200,7 +200,7 @@ def handle_request(self, request: Request) -> Response:
response = connection.handle_request(
pool_request.request
)
except (ConnectionNotAvailable, ServerDisconnectedInternalError):
except (ConnectionNotAvailable, ServerDisconnectedError):
# In some cases a connection may initially be available to
# handle a request, but then become unavailable.
#
Expand Down
12 changes: 6 additions & 6 deletions httpcore/_sync/http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ConnectionNotAvailable,
LocalProtocolError,
RemoteProtocolError,
ServerDisconnectedInternalError,
ServerDisconnectedError,
WriteError,
map_exceptions,
)
Expand Down Expand Up @@ -79,19 +79,19 @@ def handle_request(self, request: Request) -> Response:
)

with self._state_lock:
if self._state == HTTPConnectionState.SERVER_DISCONNECTED:
raise ServerDisconnectedError()

# If the HTTP connection is idle but the socket is readable, then the
# only valid state is that the socket is about to return b"", indicating
# a server-initiated disconnect.
server_disconnected = (
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
if (
server_disconnected
or self._state == HTTPConnectionState.SERVER_DISCONNECTED
):
if server_disconnected:
self._state = HTTPConnectionState.SERVER_DISCONNECTED
raise ServerDisconnectedInternalError()
raise ServerDisconnectedError()

if self._state in (HTTPConnectionState.NEW, HTTPConnectionState.IDLE):
self._request_count += 1
Expand Down
7 changes: 5 additions & 2 deletions tests/_async/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

import httpcore
from httpcore._exceptions import ServerDisconnectedInternalError
from httpcore._exceptions import ServerDisconnectedError


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

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

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


Expand Down
7 changes: 5 additions & 2 deletions tests/_sync/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

import httpcore
from httpcore._exceptions import ServerDisconnectedInternalError
from httpcore._exceptions import ServerDisconnectedError



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

with pytest.raises(ServerDisconnectedInternalError):
with pytest.raises(ServerDisconnectedError):
conn.request("GET", "https://example.com/")
assert conn.has_expired() and not conn.is_idle()

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


Expand Down

0 comments on commit e3778a4

Please sign in to comment.