yf176 commited on
Commit ·
73ea58f
1
Parent(s): 55e43b7
fix: redact Finnhub provider errors
Browse files
src/polyhedra/services/finnhub.py
CHANGED
|
@@ -25,6 +25,8 @@ from typing import Any
|
|
| 25 |
|
| 26 |
import httpx
|
| 27 |
|
|
|
|
|
|
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
_BASE_URL = "https://finnhub.io/api/v1"
|
|
@@ -118,8 +120,9 @@ class FinnhubService:
|
|
| 118 |
try:
|
| 119 |
ohlcv_result = await self._fetch_ohlcv_finnhub(ticker)
|
| 120 |
except Exception as exc:
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
else:
|
| 124 |
combined["_CANDLE_error"] = (
|
| 125 |
"FINNHUB_API_KEY is not configured; using yfinance OHLCV fallback"
|
|
@@ -165,8 +168,9 @@ class FinnhubService:
|
|
| 165 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
| 166 |
return combined
|
| 167 |
except Exception as exc:
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
# --- Earnings ---
|
| 172 |
try:
|
|
@@ -182,8 +186,9 @@ class FinnhubService:
|
|
| 182 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
| 183 |
return combined
|
| 184 |
except Exception as exc:
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
|
| 188 |
if out_path is not None:
|
| 189 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
|
|
|
| 25 |
|
| 26 |
import httpx
|
| 27 |
|
| 28 |
+
from polyhedra.services.error_sanitizer import sanitize_error_text
|
| 29 |
+
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
| 32 |
_BASE_URL = "https://finnhub.io/api/v1"
|
|
|
|
| 120 |
try:
|
| 121 |
ohlcv_result = await self._fetch_ohlcv_finnhub(ticker)
|
| 122 |
except Exception as exc:
|
| 123 |
+
safe_error = sanitize_error_text(exc)
|
| 124 |
+
logger.warning("Finnhub OHLCV for %s failed: %s", ticker, safe_error)
|
| 125 |
+
combined["_CANDLE_error"] = safe_error
|
| 126 |
else:
|
| 127 |
combined["_CANDLE_error"] = (
|
| 128 |
"FINNHUB_API_KEY is not configured; using yfinance OHLCV fallback"
|
|
|
|
| 168 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
| 169 |
return combined
|
| 170 |
except Exception as exc:
|
| 171 |
+
safe_error = sanitize_error_text(exc)
|
| 172 |
+
logger.warning("Finnhub NEWS for %s failed: %s", ticker, safe_error)
|
| 173 |
+
combined["_NEWS_error"] = safe_error
|
| 174 |
|
| 175 |
# --- Earnings ---
|
| 176 |
try:
|
|
|
|
| 186 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
| 187 |
return combined
|
| 188 |
except Exception as exc:
|
| 189 |
+
safe_error = sanitize_error_text(exc)
|
| 190 |
+
logger.warning("Finnhub EARNINGS for %s failed: %s", ticker, safe_error)
|
| 191 |
+
combined["_EARNINGS_error"] = safe_error
|
| 192 |
|
| 193 |
if out_path is not None:
|
| 194 |
out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
|
tests/test_finnhub_service.py
CHANGED
|
@@ -43,6 +43,14 @@ class _NoKeyFinnhub(_FakeFinnhub):
|
|
| 43 |
raise ValueError("FINNHUB_API_KEY environment variable is not set")
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def _candle_payload() -> dict[str, Any]:
|
| 47 |
return {
|
| 48 |
"s": "ok",
|
|
@@ -112,6 +120,26 @@ async def test_yfinance_fallback_works_without_finnhub_key(tmp_path):
|
|
| 112 |
assert "FINNHUB_API_KEY environment variable is not set" in data["_NEWS_error"]
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def test_app_suppresses_httpx_info_request_logs():
|
| 116 |
import polyhedra.app # noqa: F401
|
| 117 |
|
|
|
|
| 43 |
raise ValueError("FINNHUB_API_KEY environment variable is not set")
|
| 44 |
|
| 45 |
|
| 46 |
+
class _CredentialErrorFinnhub(_FakeFinnhub):
|
| 47 |
+
async def _call(self, path: str, params: dict[str, str]) -> Any:
|
| 48 |
+
raise RuntimeError(
|
| 49 |
+
"Client error for https://finnhub.io/api/v1"
|
| 50 |
+
f"{path}?symbol=SPY&token=secret-provider-token"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
def _candle_payload() -> dict[str, Any]:
|
| 55 |
return {
|
| 56 |
"s": "ok",
|
|
|
|
| 120 |
assert "FINNHUB_API_KEY environment variable is not set" in data["_NEWS_error"]
|
| 121 |
|
| 122 |
|
| 123 |
+
@pytest.mark.asyncio
|
| 124 |
+
async def test_provider_errors_redact_credentials_from_logs_and_artifacts(
|
| 125 |
+
tmp_path,
|
| 126 |
+
caplog,
|
| 127 |
+
):
|
| 128 |
+
service = _CredentialErrorFinnhub({}, yfinance_result=_yfinance_result())
|
| 129 |
+
caplog.set_level(logging.WARNING)
|
| 130 |
+
|
| 131 |
+
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 132 |
+
|
| 133 |
+
errors = " ".join(str(data.get(key) or "") for key in (
|
| 134 |
+
"_CANDLE_error", "_NEWS_error", "_EARNINGS_error",
|
| 135 |
+
))
|
| 136 |
+
log_text = " ".join(record.getMessage() for record in caplog.records)
|
| 137 |
+
assert "secret-provider-token" not in errors
|
| 138 |
+
assert "secret-provider-token" not in log_text
|
| 139 |
+
assert "token=<redacted>" in errors
|
| 140 |
+
assert "token=<redacted>" in log_text
|
| 141 |
+
|
| 142 |
+
|
| 143 |
def test_app_suppresses_httpx_info_request_logs():
|
| 144 |
import polyhedra.app # noqa: F401
|
| 145 |
|