Skip to content

Commit ed70e0e

Browse files
lesnik512claude
andcommitted
fix(errors): make RetryBudgetExhaustedError picklable + tighten str() assertion
Keyword-only __init__ breaks Python's default exception pickle protocol (unpickle calls cls(str_message) positionally). Mirror StatusError's pattern: add _reconstruct_budget_exhausted + __reduce__. A budget-exhausted error is the most likely error to cross process boundaries (the budget exists to protect against load), so silently losing diagnostic context via UnpicklingError would be bad. Also tightens the summary-message test from "'5' in str(exc)" to an exact-string match — the digit-in-string check would pass for many wrong messages. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f383fc0 commit ed70e0e

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/httpware/errors.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ class ServiceUnavailableError(ServerStatusError):
146146
}
147147

148148

149+
def _reconstruct_budget_exhausted(
150+
cls: "type[RetryBudgetExhaustedError]",
151+
last_response: httpx2.Response | None,
152+
last_exception: BaseException | None,
153+
attempts: int,
154+
) -> "RetryBudgetExhaustedError":
155+
return cls(last_response=last_response, last_exception=last_exception, attempts=attempts)
156+
157+
149158
class RetryBudgetExhaustedError(ClientError):
150159
"""Raised when a retry was needed but the RetryBudget refused to permit it.
151160
@@ -168,3 +177,9 @@ def __init__(
168177
self.last_exception = last_exception
169178
self.attempts = attempts
170179
super().__init__(f"retry budget exhausted after {attempts} attempt(s)")
180+
181+
def __reduce__(self) -> tuple[Any, ...]:
182+
return (
183+
_reconstruct_budget_exhausted,
184+
(type(self), self.last_response, self.last_exception, self.attempts),
185+
)

‎tests/test_errors.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,22 @@ def test_retry_budget_exhausted_error_carries_last_response_and_exception() -> N
189189

190190
def test_retry_budget_exhausted_error_summary_mentions_attempts() -> None:
191191
exc = RetryBudgetExhaustedError(last_response=None, last_exception=None, attempts=_RETRY_ATTEMPTS_5)
192-
assert str(_RETRY_ATTEMPTS_5) in str(exc)
192+
assert str(exc) == f"retry budget exhausted after {_RETRY_ATTEMPTS_5} attempt(s)"
193+
194+
195+
_SERVICE_UNAVAILABLE = 503
196+
197+
198+
def test_retry_budget_exhausted_error_pickleable() -> None:
199+
response = _make_response(_SERVICE_UNAVAILABLE, url="https://example.test/x")
200+
inner = RuntimeError("boom")
201+
exc = RetryBudgetExhaustedError(
202+
last_response=response,
203+
last_exception=inner,
204+
attempts=_RETRY_ATTEMPTS_3,
205+
)
206+
restored = pickle.loads(pickle.dumps(exc)) # noqa: S301
207+
assert isinstance(restored, RetryBudgetExhaustedError)
208+
assert restored.attempts == _RETRY_ATTEMPTS_3
209+
assert restored.last_response is not None
210+
assert restored.last_response.status_code == _SERVICE_UNAVAILABLE

0 commit comments

Comments
 (0)