Skip to content

Commit 1484b0a

Browse files
lesnik512claude
andcommitted
docs(planning): spec — add NetworkError prerequisite + StatusError-as-exception note
Two reality reconciliations found while drafting the plan: 1. The spec referenced httpware.ConnectError (doesn't exist). Replace with NetworkError(TransportError) — a new subclass for transient network failures only, so Retry doesn't noisily retry InvalidURL / CookieConflict. Terminal mapping refines to route transient httpx2 errors through NetworkError; bare TransportError stays for non-transient cases. 2. The AsyncClient terminal already raises StatusError subclasses on 4xx/5xx, so Retry sees an exception (not a Response with non-2xx status). Clarify the failure-type gate accordingly and explain that on exhaustion the original StatusError subclass is re-raised unwrapped. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8614486 commit 1484b0a

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

‎planning/specs/2026-06-05-retry-and-retry-budget-design.md‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ Items deliberately deferred so this slice ships clean:
1616

1717
- **No per-call retry override.** No `extensions["httpware_retry"]` key in v1. Callers with heterogeneous retry needs construct a second `AsyncClient` with different middleware. Purely additive to add later.
1818
- **No `Backoff` protocol.** Backoff is hardcoded to exponential with full jitter (AWS-recommended). Add a protocol later if a real use case emerges (YAGNI).
19-
- **No `retry_on_exception=` configuration.** The retryable-exception set is hardcoded to `(httpware.ConnectError, httpware.TimeoutError, asyncio.TimeoutError)`. Users wanting `ChunkedEncodingError`-style additions wait for v0.5.
19+
- **No `retry_on_exception=` configuration.** The retryable-exception set is hardcoded to `(httpware.NetworkError, httpware.TimeoutError, asyncio.TimeoutError)`. Users wanting `ChunkedEncodingError`-style additions wait for v0.5.
2020
- **No `Bulkhead`.** Slice C, separate spec.
2121
- **No standalone per-attempt-timeout middleware.** Folded into `Retry.attempt_timeout=` (3-1 dissolved during brainstorming — `asyncio.timeout()` and httpx2's built-in per-op timeouts cover the standalone use case).
2222
- **No status-code re-classification.** `RetryBudgetExhaustedError` and `httpware.TimeoutError` map cleanly to existing exception types — no new `_internal/` plumbing.
2323

24+
## Prerequisite refinement: `NetworkError`
25+
26+
The current `AsyncClient._terminal` maps every non-timeout `httpx2.HTTPError` (including non-transient `InvalidURL` and `CookieConflict`) to `httpware.TransportError`. Retrying on `TransportError` would noisily retry typos and bad cookies. This slice adds:
27+
28+
```python
29+
# In src/httpware/errors.py:
30+
class NetworkError(TransportError):
31+
"""Transient network-layer failure (connect / read / write / pool). Safe to retry."""
32+
```
33+
34+
And refines the terminal mapping so that `httpx2`'s transient-network exception family (`httpx2.NetworkError` per httpx convention, or whichever symbols httpx2 exposes for the same hierarchy) raises `httpware.NetworkError` rather than the broader `TransportError`. `InvalidURL` and `CookieConflict` continue to raise `TransportError` directly so they are NOT retried. Existing tests catching `TransportError` keep working (`NetworkError` is a subclass).
35+
36+
This is the single load-bearing assumption Retry depends on — without it, Retry can't distinguish transient from permanent transport-layer failures.
37+
2438
## Architecture & module layout
2539

2640
```text
@@ -131,8 +145,8 @@ For each completed attempt (exception OR response), `Retry` evaluates:
131145

132146
1. **Idempotency gate.** If `request.method.upper() not in retry_methods`, return the result as-is. POST/PATCH never retry by default.
133147
2. **Failure-type gate.** Retry IF:
134-
- the attempt raised `httpware.ConnectError`, `httpware.TimeoutError`, or `asyncio.TimeoutError`; OR
135-
- the attempt returned a response with `status_code in retry_status_codes`.
148+
- the attempt raised `httpware.NetworkError`, `httpware.TimeoutError`, or `asyncio.TimeoutError`; OR
149+
- the attempt raised an `httpware.StatusError` subclass whose `.response.status_code` is in `retry_status_codes` (since the `AsyncClient` terminal raises `StatusError` on 4xx/5xx, retryable status codes surface as exceptions, not response objects — see "Implementation note" below).
136150
3. **Attempt-count gate.** If `attempt_index + 1 >= max_attempts`, stop.
137151
4. **Budget gate.** Call `budget.try_withdraw()`. If `False`, raise `RetryBudgetExhaustedError` (see "Errors raised" below).
138152
5. **Sleep, then retry.** Compute delay via backoff (Retry-After overrides if applicable), `await self._sleep(delay)`, increment attempt index.
@@ -180,7 +194,11 @@ Rationale: putting `Retry` at the outermost position means each attempt re-runs
180194
- **`max_attempts` exhausted**: the *last* error (exception or `StatusError`) is **re-raised unwrapped**. `exc.__notes__` is appended with `"httpware: gave up after N attempts"` (PEP 678) so the gave-up-after context is preserved without changing the exception type. This keeps consumer `except SomeStatusError:` blocks working unchanged.
181195
- **`attempt_timeout` firing**: caught as `asyncio.TimeoutError` inside the retry loop, re-raised as `httpware.TimeoutError` (whether retried or surfaced as the final error). This matches the existing httpx2-error-mapping pattern in the `AsyncClient` terminal.
182196

183-
`RetryBudgetExhaustedError` lives in `src/httpware/errors.py` alongside the existing exception tree, exported from `httpware/__init__.py`.
197+
`RetryBudgetExhaustedError` and `NetworkError` both live in `src/httpware/errors.py` alongside the existing exception tree, exported from `httpware/__init__.py`.
198+
199+
### Implementation note: `StatusError` surfaces as exception, not response
200+
201+
The `AsyncClient` terminal (`client.py:106-126`) raises a `StatusError` subclass on 4xx/5xx — the middleware chain receives an exception, not a `Response` object with a non-2xx status. Retry's status-code check therefore lives in an `except StatusError as exc:` branch, inspecting `exc.response.status_code`. On exhaustion, the original `StatusError` subclass is re-raised unwrapped (preserving consumer `except NotFoundError:` patterns).
184202

185203
## Testing
186204

@@ -197,8 +215,9 @@ Per `planning/engineering.md §6` (test patterns):
197215
- `Retry-After` capped at `max_delay`
198216
- skips non-idempotent methods by default (POST returns 503 → not retried)
199217
- honors `attempt_timeout` (slow mock transport → `httpware.TimeoutError`)
200-
- retries on `httpware.ConnectError` / `httpware.TimeoutError`
218+
- retries on `httpware.NetworkError` / `httpware.TimeoutError`
201219
- does NOT retry on `httpware.NotFoundError` (404)
220+
- does NOT retry on bare `httpware.TransportError` (e.g., `InvalidURL`) — only on the `NetworkError` subclass
202221
- `RetryBudgetExhaustedError` raised when budget refuses
203222
- exhausted budget exposes `last_response` / `last_exception` / `attempts`
204223
- explicit `budget=` parameter shared across two `Retry` middlewares accumulates correctly

0 commit comments

Comments
 (0)