Skip to content

Commit e2a38bb

Browse files
lesnik512claude
andcommitted
feat(errors): add CircuitOpenError
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 29306fa commit e2a38bb

4 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎src/httpware/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
STATUS_TO_EXCEPTION,
77
BadRequestError,
88
BulkheadFullError,
9+
CircuitOpenError,
910
ClientError,
1011
ClientStatusError,
1112
ConflictError,
@@ -50,6 +51,7 @@
5051
"BadRequestError",
5152
"Bulkhead",
5253
"BulkheadFullError",
54+
"CircuitOpenError",
5355
"Client",
5456
"ClientError",
5557
"ClientStatusError",

‎src/httpware/errors.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,35 @@ def __reduce__(self) -> tuple[Any, ...]:
214214
)
215215

216216

217+
def _reconstruct_circuit_open(
218+
cls: "type[CircuitOpenError]",
219+
retry_after: float | None,
220+
) -> "CircuitOpenError":
221+
return cls(retry_after=retry_after)
222+
223+
224+
class CircuitOpenError(ClientError):
225+
"""Raised when a CircuitBreaker refuses a request because the circuit is not closed.
226+
227+
Fires when the circuit is OPEN, or when it is HALF_OPEN and the single probe
228+
slot is already taken. The request is never forwarded to ``next``. ``retry_after``
229+
carries the seconds until the circuit will next admit a probe, when known
230+
(``None`` when a concurrent probe is already in flight).
231+
"""
232+
233+
retry_after: float | None
234+
235+
def __init__(self, *, retry_after: float | None) -> None:
236+
self.retry_after = retry_after
237+
if retry_after is None:
238+
super().__init__("circuit open (a probe request is already in flight)")
239+
else:
240+
super().__init__(f"circuit open (retry_after={retry_after:.3f}s)")
241+
242+
def __reduce__(self) -> tuple[Any, ...]:
243+
return (_reconstruct_circuit_open, (type(self), self.retry_after))
244+
245+
217246
def _reconstruct_decode_error(
218247
cls: "type[DecodeError]",
219248
response: httpx2.Response,

‎tests/test_errors.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
STATUS_TO_EXCEPTION,
1313
BadRequestError,
1414
BulkheadFullError,
15+
CircuitOpenError,
1516
ClientError,
1617
ClientStatusError,
1718
ConflictError,
@@ -106,6 +107,7 @@ def test_status_error_repr_strips_userinfo() -> None:
106107

107108

108109
_NOT_FOUND = 404
110+
_RETRY_AFTER_2_5 = 2.5
109111
_RETRY_ATTEMPTS_3 = 3
110112
_RETRY_ATTEMPTS_2 = 2
111113
_RETRY_ATTEMPTS_5 = 5
@@ -357,3 +359,38 @@ def test_missing_decoder_error_pickle_roundtrip() -> None:
357359
assert isinstance(revived, MissingDecoderError)
358360
assert revived.model is _Foo
359361
assert revived.registered_names == ("PydanticDecoder", "MsgspecDecoder")
362+
363+
364+
def test_circuit_open_error_is_client_error() -> None:
365+
exc = CircuitOpenError(retry_after=_RETRY_AFTER_2_5)
366+
assert isinstance(exc, ClientError)
367+
assert exc.retry_after == _RETRY_AFTER_2_5
368+
369+
370+
def test_circuit_open_error_accepts_none_retry_after() -> None:
371+
exc = CircuitOpenError(retry_after=None)
372+
assert exc.retry_after is None
373+
374+
375+
def test_circuit_open_error_summary_with_retry_after() -> None:
376+
exc = CircuitOpenError(retry_after=_RETRY_AFTER_2_5)
377+
assert str(exc) == "circuit open (retry_after=2.500s)"
378+
379+
380+
def test_circuit_open_error_summary_with_none_retry_after() -> None:
381+
exc = CircuitOpenError(retry_after=None)
382+
assert str(exc) == "circuit open (a probe request is already in flight)"
383+
384+
385+
def test_circuit_open_error_pickleable_with_float() -> None:
386+
exc = CircuitOpenError(retry_after=_RETRY_AFTER_2_5)
387+
restored = pickle.loads(pickle.dumps(exc)) # noqa: S301
388+
assert isinstance(restored, CircuitOpenError)
389+
assert restored.retry_after == _RETRY_AFTER_2_5
390+
391+
392+
def test_circuit_open_error_pickleable_with_none() -> None:
393+
exc = CircuitOpenError(retry_after=None)
394+
restored = pickle.loads(pickle.dumps(exc)) # noqa: S301
395+
assert isinstance(restored, CircuitOpenError)
396+
assert restored.retry_after is None

‎tests/test_public_api.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_expected_exports() -> None:
3636
"BadRequestError",
3737
"Bulkhead",
3838
"BulkheadFullError",
39+
"CircuitOpenError",
3940
"Client",
4041
"ClientError",
4142
"ClientStatusError",

0 commit comments

Comments
 (0)