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(data-warehouse): handle transient Google auth errors in Sheets validation (#70211)

Tom Owers034aa1d22c1f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
index d10c89048823..279866dc993e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
@@ -3,6 +3,7 @@ from typing import Optional, cast
from django.conf import settings
import gspread
+from google.auth import exceptions as google_auth_exceptions
from posthog.schema import (
DataWarehouseSourceCategory,
@@ -124,6 +125,23 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):
"Google Sheets could not open this spreadsheet. Please check the URL and that it is shared "
"with our service account as described at https://posthog.com/docs/cdp/sources/google-sheets",
)
+ except (google_auth_exceptions.RefreshError, google_auth_exceptions.TransportError) as e:
+ # These come from fetching PostHog's own service-account OAuth token (the user only shares
+ # their sheet with our service account), not from the user's credentials. google-auth flags
+ # a transient Google-side blip as retryable; its str() is otherwise a raw server message like
+ # "A server error occurred." A non-retryable failure (e.g. invalid_grant from a rotated key)
+ # is a persistent problem on our side, so don't dress it up as merely temporary.
+ if getattr(e, "retryable", False):
+ return (
+ False,
+ "PostHog couldn't verify access to your Google Sheet right now because Google returned a "
+ "temporary error. Please try again in a moment.",
+ )
+ return (
+ False,
+ "PostHog couldn't authenticate with Google to verify access to your Google Sheet. This looks "
+ "like a problem on our side, so please contact support if it keeps happening.",
+ )
except Exception as e:
return False, str(e)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
index eedfabee3a60..6547334fb08a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
@@ -6,6 +6,7 @@ from unittest import mock
import gspread
import requests
+from google.auth import exceptions as google_auth_exceptions
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs import GoogleSheetsSourceConfig
from products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.google_sheets import (
@@ -578,3 +579,31 @@ def test_validate_credentials_maps_api_error_to_friendly_message(api_message, ex
assert expected_fragment in (error_message or "")
# The raw gspread "APIError: [400]: ..." dump must not reach the user.
assert "APIError" not in (error_message or "")
+
+
+ "auth_error,expect_retry_hint",
+ [
+ # Transient Google-side blip (token endpoint 5xx) — google-auth flags it retryable.
+ (google_auth_exceptions.RefreshError("A server error occurred.", retryable=True), True),
+ (google_auth_exceptions.TransportError("connection reset", retryable=True), True),
+ # Persistent problem with our own service-account key (e.g. invalid_grant) — not retryable,
+ # so the copy must not tell the user it's merely temporary.
+ (google_auth_exceptions.RefreshError({"error": "invalid_grant"}, retryable=False), False),
+ ],
+)
+def test_validate_credentials_maps_google_auth_error(auth_error, expect_retry_hint):
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.source.google_sheets_client"
+ ) as mock_client:
+ mock_client.return_value.open_by_url.side_effect = auth_error
+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")
+ is_valid, error_message = GoogleSheetsSource().validate_credentials(config, team_id=1)
+
+ assert is_valid is False
+ # The raw Google token-endpoint message ("A server error occurred.") must not reach the user.
+ assert "A server error occurred." not in (error_message or "")
+ if expect_retry_hint:
+ assert "try again in a moment" in (error_message or "").lower()
+ else:
+ assert "try again in a moment" not in (error_message or "").lower()