Skip to content

Commit f383fc0

Browse files
committed
feat(errors): add RetryBudgetExhaustedError
Distinct exception raised by the Retry middleware when the RetryBudget refuses to permit a retry. Carries last_response / last_exception / attempts. Inherits ClientError so callers catching ClientError already handle it.
1 parent 08ced9c commit f383fc0

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎src/httpware/errors.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,27 @@ class ServiceUnavailableError(ServerStatusError):
144144
500: InternalServerError,
145145
503: ServiceUnavailableError,
146146
}
147+
148+
149+
class RetryBudgetExhaustedError(ClientError):
150+
"""Raised when a retry was needed but the RetryBudget refused to permit it.
151+
152+
Carries the last response and/or exception observed before the budget refused,
153+
plus the number of attempts already completed.
154+
"""
155+
156+
last_response: httpx2.Response | None
157+
last_exception: BaseException | None
158+
attempts: int
159+
160+
def __init__(
161+
self,
162+
*,
163+
last_response: httpx2.Response | None,
164+
last_exception: BaseException | None,
165+
attempts: int,
166+
) -> None:
167+
self.last_response = last_response
168+
self.last_exception = last_exception
169+
self.attempts = attempts
170+
super().__init__(f"retry budget exhausted after {attempts} attempt(s)")

‎tests/test_errors.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
NetworkError,
1818
NotFoundError,
1919
RateLimitedError,
20+
RetryBudgetExhaustedError,
2021
ServerStatusError,
2122
ServiceUnavailableError,
2223
StatusError,
@@ -95,6 +96,9 @@ def test_status_error_repr_strips_userinfo() -> None:
9596

9697

9798
_NOT_FOUND = 404
99+
_RETRY_ATTEMPTS_3 = 3
100+
_RETRY_ATTEMPTS_2 = 2
101+
_RETRY_ATTEMPTS_5 = 5
98102

99103

100104
def test_status_error_pickleable() -> None:
@@ -164,3 +168,25 @@ def test_network_error_is_transport_error() -> None:
164168
exc = NetworkError("connection refused")
165169
assert isinstance(exc, TransportError)
166170
assert isinstance(exc, ClientError)
171+
172+
173+
def test_retry_budget_exhausted_error_is_client_error() -> None:
174+
exc = RetryBudgetExhaustedError(last_response=None, last_exception=None, attempts=_RETRY_ATTEMPTS_3)
175+
assert isinstance(exc, ClientError)
176+
assert exc.last_response is None
177+
assert exc.last_exception is None
178+
assert exc.attempts == _RETRY_ATTEMPTS_3
179+
180+
181+
def test_retry_budget_exhausted_error_carries_last_response_and_exception() -> None:
182+
response = _make_response(503, url="https://example.test/x")
183+
inner = RuntimeError("boom")
184+
exc = RetryBudgetExhaustedError(last_response=response, last_exception=inner, attempts=_RETRY_ATTEMPTS_2)
185+
assert exc.last_response is response
186+
assert exc.last_exception is inner
187+
assert exc.attempts == _RETRY_ATTEMPTS_2
188+
189+
190+
def test_retry_budget_exhausted_error_summary_mentions_attempts() -> None:
191+
exc = RetryBudgetExhaustedError(last_response=None, last_exception=None, attempts=_RETRY_ATTEMPTS_5)
192+
assert str(_RETRY_ATTEMPTS_5) in str(exc)

0 commit comments

Comments
 (0)