Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/courier/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,8 @@ def request(

log.debug("Raising timeout error")
raise APITimeoutError(request=request) from err
except Exception as err:
log.debug("Encountered Exception", exc_info=True)
except httpx.RequestError as err:
log.debug("Encountered httpx.RequestError", exc_info=True)

if remaining_retries > 0:
self._sleep_for_retry(
Expand Down Expand Up @@ -1607,8 +1607,8 @@ async def request(

log.debug("Raising timeout error")
raise APITimeoutError(request=request) from err
except Exception as err:
log.debug("Encountered Exception", exc_info=True)
except httpx.RequestError as err:
log.debug("Encountered httpx.RequestError", exc_info=True)

if remaining_retries > 0:
await self._sleep_for_retry(
Expand Down
40 changes: 38 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
raise httpx.ConnectError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand All @@ -920,6 +920,23 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
assert response.retries_taken == failures_before_success
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

@mock.patch("courier._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
def test_non_transport_exception_is_not_retried(self, client: Courier, respx_mock: MockRouter) -> None:
# Stands in for Celery's SoftTimeLimitExceeded, which a signal handler raises
# mid-request. It is not a network failure, so it must propagate unretried.
class TaskCancelled(Exception):
pass

client = client.with_options(max_retries=4)

respx_mock.post("/send").mock(side_effect=TaskCancelled())

with pytest.raises(TaskCancelled):
client.send.with_raw_response.message(message={})

assert len(respx_mock.calls) == 1

@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@mock.patch("courier._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
Expand Down Expand Up @@ -1841,7 +1858,7 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
if nb_retries < failures_before_success:
nb_retries += 1
if failure_mode == "exception":
raise RuntimeError("oops")
raise httpx.ConnectError("oops")
return httpx.Response(500)
return httpx.Response(200)

Expand All @@ -1852,6 +1869,25 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
assert response.retries_taken == failures_before_success
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success

@mock.patch("courier._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
async def test_non_transport_exception_is_not_retried(
self, async_client: AsyncCourier, respx_mock: MockRouter
) -> None:
# Stands in for Celery's SoftTimeLimitExceeded, which a signal handler raises
# mid-request. It is not a network failure, so it must propagate unretried.
class TaskCancelled(Exception):
pass

client = async_client.with_options(max_retries=4)

respx_mock.post("/send").mock(side_effect=TaskCancelled())

with pytest.raises(TaskCancelled):
await client.send.with_raw_response.message(message={})

assert len(respx_mock.calls) == 1

@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@mock.patch("courier._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
Expand Down
Loading