: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): tell users to reconnect when their meta ads connection expires (#117796)
Tom Owers1a94d885a74f
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 6e48d61117ff..1a2bd2d47b8b 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@@ -169,6 +169,11 @@ def _fetch_integration_row(integration_id: int, team_id: int) -> Integration:_backoff_sleep(attempt)+META_TOKEN_REFRESH_ERROR_MESSAGE = (+ "Your Meta Ads connection has expired or been revoked. Reconnect your Meta Ads account to continue."+)++class MetaAdsTokenRefreshError(Exception):"""Meta refused to refresh the integration's access token — only re-authorization fixes it."""@@ -180,9 +185,7 @@ def get_integration_by_id(integration_id: int, team_id: int) -> Integration:meta_ads_integration.refresh_access_token()if meta_ads_integration.integration.errors == ERROR_TOKEN_REFRESH_FAILED:- raise MetaAdsTokenRefreshError(- "Failed to refresh token for Meta Ads integration. Please re-authorize the integration."- )+ raise MetaAdsTokenRefreshError(META_TOKEN_REFRESH_ERROR_MESSAGE)return meta_ads_integration.integrationdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.pyindex 953c5ea476b2..12f32e2c01ea 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/meta_ads/source.py@@ -47,6 +47,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.mMETA_AUTH_ERROR_MESSAGE,META_INVALID_CURSOR_ERROR_MESSAGE,META_RATE_LIMIT_ERROR_MESSAGE,+ META_TOKEN_REFRESH_ERROR_MESSAGE,SHRINK_EXHAUSTED_ERROR_MESSAGE,MetaAdsAuthError,MetaAdsRateLimitError,@@ -133,7 +134,7 @@ class MetaAdsSource(ResumableSource[MetaAdsSourceConfig, MetaAdsResumeConfig], Odef get_non_retryable_errors(self) -> dict[str, str | None]:return {- "Failed to refresh token for Meta Ads integration. Please re-authorize the integration.": None,+ META_TOKEN_REFRESH_ERROR_MESSAGE: None,# The data warehouse source still references a `meta_ads_integration_id` whose# Integration row no longer exists for the team (the integration was deleted or# de-authorized). `get_integration` then raises Django's `Integration.DoesNotExist`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.pyindex 2d8b94e5a4e4..6554e69c72ed 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@@ -44,6 +44,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.mMETA_AUTH_ERROR_MESSAGE,META_INVALID_CURSOR_ERROR_MESSAGE,META_RATE_LIMIT_ERROR_MESSAGE,+ META_TOKEN_REFRESH_ERROR_MESSAGE,META_TRANSIENT_ERROR_MAX_ATTEMPTS,PAGE_LIMIT_FALLBACK_SIZES,SHRINK_EXHAUSTED_ERROR_MESSAGE,@@ -1401,7 +1402,7 @@ class TestNonRetryableErrors:"error_message",[# Token refresh failure raised by get_integration.- "Failed to refresh token for Meta Ads integration. Please re-authorize the integration.",+ META_TOKEN_REFRESH_ERROR_MESSAGE,# Integration row deleted/de-authorized while the source still references it —# get_integration raises Django's Integration.DoesNotExist with this message."Integration matching query does not exist.",diff --git a/products/warehouse_sources/backend/tests/api/test_external_data_source.py b/products/warehouse_sources/backend/tests/api/test_external_data_source.pyindex b7b86904a254..dbe055d4bb45 100644--- a/products/warehouse_sources/backend/tests/api/test_external_data_source.py+++ b/products/warehouse_sources/backend/tests/api/test_external_data_source.py@@ -102,6 +102,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.custom.sou)from products.warehouse_sources.backend.temporal.data_imports.sources.linkedin_ads.client import LinkedinAdsApiErrorfrom products.warehouse_sources.backend.temporal.data_imports.sources.meta_ads.meta_ads import (+ META_TOKEN_REFRESH_ERROR_MESSAGE,MetaAdsRateLimitError,MetaAdsTokenRefreshError,)@@ -14109,14 +14110,12 @@ class TestOAuthAccountsEndpoint(APIBaseTest):# Meta refusing to refresh the stored token is a customer-side state (revoked access), so it# must reach the user as an actionable 400 rather than an unhandled 500.integration = self._meta_ads_integration()- error = MetaAdsTokenRefreshError(- "Failed to refresh token for Meta Ads integration. Please re-authorize the integration."- )+ error = MetaAdsTokenRefreshError(META_TOKEN_REFRESH_ERROR_MESSAGE)with patch(f"{self._META_ADS_MODULE}.get_integration_by_id", side_effect=error):response = self.client.get(self._url("MetaAds", integration.id))assert response.status_code == status.HTTP_400_BAD_REQUEST, response.content- assert "re-authorize" in str(response.json()).lower()+ assert META_TOKEN_REFRESH_ERROR_MESSAGE in str(response.json())def test_meta_ads_rate_limit_returns_actionable_400(self):# Meta throttling us is not a bug — it must not page us as a 500. The user just retries.