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(google_ads): treat OAuth invalid_grant as non-retryable (#56470)

Tom Owers6ca32e41aa71
diff --git a/posthog/temporal/data_imports/sources/google_ads/source.py b/posthog/temporal/data_imports/sources/google_ads/source.py
index 99d4a592c3c9..66a97bfac749 100644
--- a/posthog/temporal/data_imports/sources/google_ads/source.py
+++ b/posthog/temporal/data_imports/sources/google_ads/source.py
@@ -50,11 +50,10 @@ 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.",
+ # google.auth.exceptions.RefreshError raised when the stored OAuth refresh token
+ # has been revoked, expired, or is otherwise rejected by Google's token endpoint.
+ # Retrying cannot recover — the user must reconnect their Google Ads account.
+ "invalid_grant": "Your Google Ads connection 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
index 89b5e1fa6666..c0790c874316 100644
--- 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
@@ -6,16 +6,27 @@ from posthog.temporal.data_imports.sources.google_ads.source import GoogleAdsSou
class TestGoogleAdsNonRetryableErrors:
def setup_method(self):
self.source = GoogleAdsSource()
+ self.non_retryable = self.source.get_non_retryable_errors()
+
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Real RefreshError string observed in production when the refresh
+ # token has been revoked / expired — reported by `str(e)` on
+ # google.auth.exceptions.RefreshError.
+ "('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})",
+ "('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})",
+ "('invalid_grant: Invalid grant: account not found', {'error': 'invalid_grant', 'error_description': 'Invalid grant: account not found'})",
+ ],
+ )
+ def test_invalid_grant_is_non_retryable(self, error_msg):
+ assert any(pattern in error_msg for pattern in self.non_retryable), (
+ f"RefreshError message {error_msg!r} did not match any non-retryable pattern"
+ )
@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",
@@ -25,8 +36,7 @@ class TestGoogleAdsNonRetryableErrors:
],
)
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())
+ is_non_retryable = any(pattern in error_msg for pattern in self.non_retryable.keys())
assert is_non_retryable, f"Expected error to be non-retryable: {error_msg}"
@pytest.mark.parametrize(
@@ -40,13 +50,24 @@ class TestGoogleAdsNonRetryableErrors:
],
)
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())
+ is_non_retryable = any(pattern in error_msg for pattern in self.non_retryable.keys())
assert not is_non_retryable, f"Expected error to be retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "pattern",
+ [
+ "PERMISSION_DENIED",
+ "UNAUTHENTICATED",
+ "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
+ "Account has been deleted",
+ "INVALID_CUSTOMER_ID",
+ "invalid_grant",
+ ],
+ )
+ def test_documented_patterns_present(self, pattern):
+ assert pattern in self.non_retryable
+
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"]
+ friendly = self.non_retryable["invalid_grant"]
assert friendly is not None
assert "reconnect" in friendly.lower()