Skip to content

Commit 515d5c4

Browse files
goranaivdusek
andauthored
fix(http-clients): Map proxy errors in stream() to ProxyError (#2529)
### Description - `HttpxHttpClient.stream()` and `ImpitHttpClient.stream()` only mapped timeouts, so a proxy failure escaped as `httpx2.ProxyError` or `impit.ProxyError`. They now map it to `crawlee.errors.ProxyError` with the same `_is_proxy_error` check that `crawl()` and `send_request()` of each client use, and that `CurlImpersonateHttpClient.stream()` already follows. `HttpClient.stream()` already documents this error. - In all three clients, the `ProxyError` from `stream()` carries the message of the library error, so the sitemap retry warning shows the cause (for example `403 Forbidden`). - `ProxyError` is a `SessionError`, so `FileDownloadCrawler(stream=True)` now rotates the session on a bad proxy instead of spending a request retry. - Adds `test_stream_with_proxy_disabled`, next to the existing `crawl` and `send_request` versions. It also checks that the error has a message. Behavior change: a proxy failure in `stream()` of the httpx and impit clients is now a `ProxyError` (a `SessionError`), not the library's own exception. Code that caught `httpx2.ProxyError` or `impit.ProxyError` around `stream()` needs to catch `crawlee.errors.ProxyError`. ### Issues - Closes: #2528 ### Testing - `pytest tests/unit/http_clients`: 17 passed. `pytest tests/integration/http_clients/test_http_clients.py -k "stream or proxy"` on Windows 11, Python 3.14: 30 passed, 18 skipped (the proxy tests are skipped on Windows by the existing marker). - With the skip marker of `test_stream_with_proxy_disabled` removed locally: 3 passed (httpx, impit, curl). With only the source change reverted: 2 failed (httpx, impit), curl passed. - Compared `crawl`, `send_request` and `stream` for 276 generated errors (httpx2 and impit exception classes, plus `RuntimeError`, `OSError` and `CancelledError`, crossed with 12 messages that include the `ROTATE_PROXY_ERRORS` needles). Before: 88 disagreements. After: 0. Timeouts, connect and read errors, other transport errors and non-transport errors still come out as before. - `FileDownloadCrawler(stream=True)` with a proxy that answers `CONNECT` with 403: before, `httpx2.ProxyError` / `impit.ProxyError`, `retry_count=2`, no session rotation. After, `crawlee.errors.ProxyError`, `retry_count=0`, 2 session rotations. - `ruff check`, `ruff format --check` and `ty check src/crawlee/http_clients` pass. ### Checklist - [x] CI passed Validated with Claude Opus 5.5. --------- Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
1 parent 843d5f6 commit 515d5c4

4 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎src/crawlee/http_clients/_curl_impersonate.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ async def stream(
285285
raise asyncio.TimeoutError from exc
286286
except CurlRequestError as exc:
287287
if self._is_proxy_error(exc):
288-
raise ProxyError from exc
288+
raise ProxyError(str(exc)) from exc
289289
raise
290290

291291
if self._persist_cookies_per_session and session and response.curl:

‎src/crawlee/http_clients/_httpx.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ async def stream(
293293
response = await client.send(http_request, stream=True)
294294
except httpx2.TimeoutException as exc:
295295
raise asyncio.TimeoutError from exc
296+
except httpx2.TransportError as exc:
297+
if self._is_proxy_error(exc):
298+
raise ProxyError(str(exc)) from exc
299+
raise
296300

297301
try:
298302
yield _HttpxResponse(response)

‎src/crawlee/http_clients/_impit.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ async def stream(
263263
)
264264
except TimeoutException as exc:
265265
raise asyncio.TimeoutError from exc
266+
except (TransportError, HTTPError) as exc:
267+
if self._is_proxy_error(exc):
268+
raise ProxyError(str(exc)) from exc
269+
raise
266270

267271
try:
268272
yield _ImpitResponse(response)

‎tests/integration/http_clients/test_http_clients.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ async def test_send_request_with_proxy_disabled(
154154
await http_client.send_request(url, proxy_info=disabled_proxy)
155155

156156

157+
@pytest.mark.skipif(os.name == 'nt', reason='Skipped on Windows')
158+
async def test_stream_with_proxy_disabled(
159+
http_client: HttpClient,
160+
disabled_proxy: ProxyInfo,
161+
) -> None:
162+
url = 'https://apify.com/'
163+
164+
with pytest.raises(ProxyError) as exc_info:
165+
async with http_client.stream(url, proxy_info=disabled_proxy):
166+
pass
167+
168+
assert str(exc_info.value)
169+
170+
157171
@pytest.mark.skipif(os.name == 'nt', reason='Skipped on Windows')
158172
async def test_session_cookies_sent_through_proxy(
159173
http_client: HttpClient,

0 commit comments

Comments
 (0)