Skip to content

Commit 478fa37

Browse files
committed
feat(errors): add NetworkError(TransportError) for transient httpx2 failures
Refines _terminal so httpx2.NetworkError-family exceptions (ConnectError, ReadError, WriteError, PoolTimeout) map to httpware.NetworkError. InvalidURL and CookieConflict stay bare TransportError. Prerequisite for the Retry middleware so it can retry transient failures without retrying typos.
1 parent 5318eaf commit 478fa37

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/httpware/client.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from httpware.errors import (
1212
STATUS_TO_EXCEPTION,
1313
ClientStatusError,
14+
NetworkError,
1415
ServerStatusError,
1516
TimeoutError, # noqa: A004
1617
TransportError,
@@ -110,6 +111,8 @@ async def _terminal(self, request: httpx2.Request) -> httpx2.Response:
110111
raise TimeoutError(str(exc)) from exc
111112
except (httpx2.InvalidURL, httpx2.CookieConflict) as exc:
112113
raise TransportError(str(exc)) from exc
114+
except httpx2.NetworkError as exc:
115+
raise NetworkError(str(exc)) from exc
113116
except httpx2.HTTPError as exc:
114117
raise TransportError(str(exc)) from exc
115118
except RuntimeError as exc:

‎src/httpware/errors.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class TransportError(ClientError):
4040
"""Connection / network / protocol failure raised before a response was received."""
4141

4242

43+
class NetworkError(TransportError):
44+
"""Transient network-layer failure (connect/read/write/pool). Safe to retry."""
45+
46+
4347
class TimeoutError(ClientError, builtins.TimeoutError): # noqa: A001
4448
"""Client-side timeout (connect / read / write / pool).
4549

‎tests/test_error_mapping_terminal.py‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TimeoutError, # noqa: A004
1818
TransportError,
1919
)
20+
from httpware.errors import NetworkError
2021

2122

2223
def _client_with_handler(handler) -> AsyncClient: # noqa: ANN001
@@ -81,13 +82,13 @@ def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
8182
await client.send(httpx2.Request("GET", "https://example.test/x"))
8283

8384

84-
async def test_httpx2_connect_error_maps_to_transport_error() -> None:
85+
async def test_httpx2_connect_error_maps_to_network_error() -> None:
8586
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
8687
msg = "connect refused"
8788
raise httpx2.ConnectError(msg)
8889

8990
client = _client_with_handler(handler)
90-
with pytest.raises(TransportError, match="connect refused"):
91+
with pytest.raises(NetworkError, match="connect refused"):
9192
await client.send(httpx2.Request("GET", "https://example.test/x"))
9293

9394

@@ -108,3 +109,29 @@ async def test_send_on_closed_client_raises_transport_error() -> None:
108109
await underlying.aclose()
109110
with pytest.raises(TransportError):
110111
await client.send(httpx2.Request("GET", "https://example.test/x"))
112+
113+
114+
async def test_httpx2_decoding_error_maps_to_transport_error() -> None:
115+
"""Non-transient HTTPError (e.g. DecodingError) maps to bare TransportError, not NetworkError."""
116+
117+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
118+
msg = "decoding failed"
119+
raise httpx2.DecodingError(msg)
120+
121+
client = _client_with_handler(handler)
122+
with pytest.raises(TransportError) as info:
123+
await client.send(httpx2.Request("GET", "https://example.test/x"))
124+
assert not isinstance(info.value, NetworkError)
125+
126+
127+
async def test_httpx2_invalid_url_does_not_map_to_network_error() -> None:
128+
"""Regression: only transient errors map to NetworkError; InvalidURL stays bare TransportError."""
129+
130+
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
131+
msg = "bad url"
132+
raise httpx2.InvalidURL(msg)
133+
134+
client = _client_with_handler(handler)
135+
with pytest.raises(TransportError) as info:
136+
await client.send(httpx2.Request("GET", "https://example.test/x"))
137+
assert not isinstance(info.value, NetworkError)

‎tests/test_errors.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ConflictError,
1515
ForbiddenError,
1616
InternalServerError,
17+
NetworkError,
1718
NotFoundError,
1819
RateLimitedError,
1920
ServerStatusError,
@@ -157,3 +158,9 @@ def test_timeout_error_is_builtin_timeout_error() -> None:
157158
def test_transport_error_is_client_error() -> None:
158159
exc = TransportError("connection refused")
159160
assert isinstance(exc, ClientError)
161+
162+
163+
def test_network_error_is_transport_error() -> None:
164+
exc = NetworkError("connection refused")
165+
assert isinstance(exc, TransportError)
166+
assert isinstance(exc, ClientError)

0 commit comments

Comments
 (0)