Skip to content

: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.

fix(meta-ads): classify a 429 with no JSON body as rate limiting (#124245)

Tom Owers72f0459a47a0
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.py
index c8c9060f08d2..78b08105b4ab 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
@@ -607,7 +607,14 @@ def _is_permanent_auth_error(response: Response) -> bool:
def _is_rate_limit_error(response: Response) -> bool:
- """Return True for Meta errors that mean "too many requests", which only waiting fixes."""
+ """Return True for Meta errors that mean "too many requests", which only waiting fixes.
+
+ A fronting proxy in front of the Graph API has been observed to answer throttling with a
+ bare HTTP 429 and an HTML error page instead of Meta's own JSON error body, so the status
+ code alone is also treated as the signal — ``_meta_error_code`` has nothing to parse there.
+ """
+ if response.status_code == 429:
+ return True
return _meta_error_code(response) in META_RATE_LIMIT_ERROR_CODES
diff --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.py
index 5303ff50cb99..6f2c823ce6b5 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
@@ -53,6 +53,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.m
SHRINK_EXHAUSTED_ERROR_MESSAGE,
SMALLEST_PAGE_LIMIT_MAX_RETRIES,
MetaAdsAuthError,
+ MetaAdsRateLimitError,
MetaAdsResumeConfig,
_earliest_supported_since,
_fetch_integration_row,
@@ -1895,6 +1896,21 @@ class TestRetryableErrors:
_raise_meta_api_error(response)
assert any(pattern in str(exc_info.value) for pattern in patterns)
+ def test_html_429_matches_rate_limit_pattern(self) -> None:
+ # A fronting proxy has been observed answering throttling with a bare HTTP 429 and an
+ # HTML error page instead of Meta's own JSON error body. With no error code to parse,
+ # this must still be tagged as rate-limiting rather than falling through to the generic,
+ # unclassified message on every attempt.
+ response = mock.MagicMock()
+ response.status_code = 429
+ response.json.side_effect = RequestsJSONDecodeError("Expecting value", "", 0)
+ response.text = "<!DOCTYPE html><html><body><h1>4xx Client Error</h1></body></html>"
+
+ 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)
+
@pytest.mark.parametrize(
"error_message,expected_fragment",
[
@@ -2186,6 +2202,17 @@ class TestListAdAccounts:
with pytest.raises(IntegrationAccountListingError, match=META_AD_ACCOUNT_LISTING_UNAVAILABLE_MESSAGE):
list_ad_accounts(self._integration())
+ def test_html_429_raises_meta_ads_rate_limit_error(self) -> None:
+ # Before the HTTP-429 fallback in `_is_rate_limit_error`, this fell through to the
+ # generic, unclassified error instead of `MetaAdsRateLimitError` (which
+ # `get_oauth_accounts` turns into the actionable message the picker shows). A 429 is in
+ # the adapter's retry status_forcelist, so `always()` answers every retried attempt.
+ with scripted_network(
+ always(ScriptedResponse(status=429, body=b"<!DOCTYPE html><html><body>4xx Client Error</body></html>"))
+ ):
+ with pytest.raises(MetaAdsRateLimitError, match=META_RATE_LIMIT_ERROR_MESSAGE):
+ list_ad_accounts(self._integration())
+
def test_page_limit_exceeded_fails_closed_instead_of_returning_partial(self) -> None:
# Meta keeps handing back a fresh, non-empty `next` cursor past the cap. Returning the
# accumulated (truncated) accounts would present them to the picker as the complete set.