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.

feat(google_ads): treat OAuth invalid_grant as non-retryable (#56474)

Tom Owersb6a018b01724
diff --git a/posthog/temporal/data_imports/sources/google_ads/source.py b/posthog/temporal/data_imports/sources/google_ads/source.py
index 3a24be32b277..99d4a592c3c9 100644
--- a/posthog/temporal/data_imports/sources/google_ads/source.py
+++ b/posthog/temporal/data_imports/sources/google_ads/source.py
@@ -50,6 +50,11 @@ class GoogleAdsSource(
"ACCESS_TOKEN_SCOPE_INSUFFICIENT": None,
"Account has been deleted": None,
"INVALID_CUSTOMER_ID": None,
+ # Google returns `invalid_grant` from the OAuth token endpoint when the user's
+ # refresh token has been revoked, expired, or otherwise invalidated (common
+ # variants: "Token has been expired or revoked.", "Bad Request"). Only the user
+ # can fix this by reconnecting their Google Ads account.
+ "invalid_grant": "Your Google Ads authorization has expired or been revoked. Please reconnect your Google Ads account.",
}
# TODO: clean up google ads source to not have two auth config options
diff --git a/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
new file mode 100644
index 000000000000..89b5e1fa6666
--- /dev/null
+++ b/posthog/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py
@@ -0,0 +1,52 @@
+import pytest
+
+from posthog.temporal.data_imports.sources.google_ads.source import GoogleAdsSource
+
+
+class TestGoogleAdsNonRetryableErrors:
+ def setup_method(self):
+ self.source = GoogleAdsSource()
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Exact excerpt reported by error tracking.
+ "RefreshError: ('invalid_grant: Token has been expired or revoked.', "
+ "{'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})",
+ # Second common variant from error tracking (same root cause — revoked/expired refresh token).
+ "RefreshError: ('invalid_grant: Bad Request', "
+ "{'error': 'invalid_grant', 'error_description': 'Bad Request'})",
+ # Other Google Ads specific failures that should stop retrying.
+ "PERMISSION_DENIED: The caller does not have permission",
+ "UNAUTHENTICATED: Request had invalid authentication credentials",
+ "ACCESS_TOKEN_SCOPE_INSUFFICIENT: Request had insufficient authentication scopes",
+ "Customer: Account has been deleted",
+ "INVALID_CUSTOMER_ID: Customer ID is not valid",
+ ],
+ )
+ def test_permanent_auth_errors_are_non_retryable(self, error_msg):
+ non_retryable = self.source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"Expected error to be non-retryable: {error_msg}"
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Transient network/infrastructure errors should still be retried.
+ "DeadlineExceeded: 504 Deadline Exceeded",
+ "UNAVAILABLE: The service is currently unavailable",
+ "ConnectionError: Connection reset by peer",
+ "INTERNAL: Internal server error",
+ ],
+ )
+ def test_transient_errors_are_retryable(self, error_msg):
+ non_retryable = self.source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert not is_non_retryable, f"Expected error to be retryable: {error_msg}"
+
+ def test_invalid_grant_has_friendly_message(self):
+ non_retryable = self.source.get_non_retryable_errors()
+ assert "invalid_grant" in non_retryable
+ friendly = non_retryable["invalid_grant"]
+ assert friendly is not None
+ assert "reconnect" in friendly.lower()