:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(meta_ads): retry on bare 5xx responses with no parseable body (#79087)
Tom Owersff0b887be625
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.pyindex 60cd96a761a7..8df4c664c77f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/meta_ads.py@@ -339,7 +339,10 @@ def _is_transient_error(response: Response) -> bool:try:error = response.json().get("error", {})except (ValueError, AttributeError):- return False+ # A 5xx with no parseable body (occasionally a completely empty response) carries no+ # error code to classify by, but a bare server-side failure is itself the signature of+ # a momentary blip — unlike a 4xx, which more likely reflects a bad request of ours.+ return response.status_code >= 500return error.get("is_transient") is True or error.get("code") in META_TRANSIENT_ERROR_CODESdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.pyindex d89402b10617..5d08e089018c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/test_meta_ads.py@@ -420,11 +420,21 @@ class TestIsTransientError:def test_reads_transient_flag_or_code(self, body: dict, expected: bool) -> None:assert _is_transient_error(_mock_response(500, body)) is expected- def test_non_json_body_is_not_transient(self) -> None:- # A proxy/gateway can return a non-JSON error page; the flag check must not crash on it.+ def test_non_json_5xx_body_is_transient(self) -> None:+ # A proxy/gateway can return a 5xx with no parseable body at all (an empty response, or+ # a non-JSON error page); the flag check must not crash on it, and a bare server-side+ # failure with no diagnostic body is itself the signature of a momentary blip.response = mock.MagicMock()response.status_code = 500response.json.side_effect = RequestsJSONDecodeError("Expecting value", "<html>", 0)+ assert _is_transient_error(response) is True++ def test_non_json_4xx_body_is_not_transient(self) -> None:+ # A 4xx more likely reflects a bad request of ours than a backend blip, so an+ # unparseable body there must not be swept into the transient-retry path.+ response = mock.MagicMock()+ response.status_code = 400+ response.json.side_effect = RequestsJSONDecodeError("Expecting value", "<html>", 0)assert _is_transient_error(response) is False@@ -1333,6 +1343,20 @@ class TestRetryableErrors:_raise_meta_api_error(_mock_response(500, body))assert any(pattern in str(exc_info.value) for pattern in patterns)+ def test_empty_body_500_matches_retryable_pattern(self) -> None:+ # Meta (or a fronting proxy) occasionally returns a bare 500 with an empty body — no+ # JSON, no error code to classify by. It must still be tagged retryable rather than+ # surfacing as a raw, unclassified failure on every attempt.+ response = mock.MagicMock()+ response.status_code = 500+ response.json.side_effect = RequestsJSONDecodeError("Expecting value", "", 0)+ response.text = ""++ patterns = MetaAdsSource().get_retryable_errors()+ with pytest.raises(Exception) as exc_info:+ _raise_meta_api_error(response)+ assert any(pattern in str(exc_info.value) for pattern in patterns)+def test_too_much_data_timeout_does_not_match_retryable_pattern(self) -> None:# The too-much-data timeout keeps its own non-retryable classification (adaptive chunking# already exhausted) — plain retries never resolve it, so it must not also be tagged