Python
fix(data): catch http.client transport errors in StockTwits
Yijia-Xiao308757c9997e
diff --git a/tests/test_stocktwits_resilience.py b/tests/test_stocktwits_resilience.pynew file mode 100644index 000000000..145e2267d--- /dev/null+++ b/tests/test_stocktwits_resilience.py@@ -0,0 +1,42 @@+"""StockTwits fetch degrades (never raises) on transport errors, including the+http.client chunked-transfer exceptions that are not OSErrors (#1024)."""++from __future__ import annotations++import http.client+from unittest.mock import patch+from urllib.error import HTTPError++import pytest++from tradingagents.dataflows import stocktwits+++def _raise(exc):+ class _Resp:+ def __enter__(self_inner):+ return self_inner++ def __exit__(self_inner, *a):+ return False++ def read(self_inner):+ raise exc+ return _Resp()+++class StockTwitsResilienceTests:+ @pytest.mark.parametrize(+ "exc",+ [+ http.client.IncompleteRead(b""),+ HTTPError("url", 503, "down", {}, None),+ TimeoutError("slow"),+ ],+ )+ def test_transport_errors_return_placeholder(self, exc):+ with patch.object(stocktwits, "urlopen", return_value=_raise(exc)):+ out = stocktwits.fetch_stocktwits_messages("NVDA")+ assert "unavailable" in out.lower()+ assert out.startswith("<stocktwits unavailable")diff --git a/tradingagents/dataflows/stocktwits.py b/tradingagents/dataflows/stocktwits.pyindex a1b2992ba..1594de1d3 100644--- a/tradingagents/dataflows/stocktwits.py+++ b/tradingagents/dataflows/stocktwits.py@@ -14,11 +14,9 @@ network call succeeded.from __future__ import annotations+import http.clientimport jsonimport logging-from datetime import datetime, timezone-from typing import Optional-from urllib.error import HTTPError, URLErrorfrom urllib.request import Request, urlopenlogger = logging.getLogger(__name__)@@ -40,7 +38,9 @@ def fetch_stocktwits_messages(ticker: str, limit: int = 30, timeout: float = 10.try:with urlopen(req, timeout=timeout) as resp:data = json.loads(resp.read())- except (HTTPError, URLError, json.JSONDecodeError, TimeoutError) as exc:+ except (OSError, http.client.HTTPException, json.JSONDecodeError) as exc:+ # OSError covers URLError/TimeoutError/connection resets; HTTPException+ # covers chunked-transfer errors (IncompleteRead/BadStatusLine, #1024).logger.warning("StockTwits fetch failed for %s: %s", ticker, exc)return f"<stocktwits unavailable: {type(exc).__name__}>"