: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(google-sheets): map Sheets API errors to an actionable message (#67676)
Tom Owersf64f325ecba0
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.pyindex 151306fbce52..005a2787dd1d 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@@ -102,6 +102,22 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):False,"Permissions missing from spreadsheet. View documentation at https://posthog.com/docs/cdp/sources/google-sheets",)+ except gspread.exceptions.APIError as e:+ # gspread stringifies these as "APIError: [<code>]: <message>", which isn't actionable.+ # The common case is an uploaded Office file (.xlsx) the Sheets API can't read.+ api_message = str(e.error.get("message", "")) if isinstance(e.error, dict) else ""+ if "Office file" in api_message:+ return (+ False,+ "This spreadsheet is an uploaded Office file (e.g. .xlsx), which the Google Sheets API "+ "can't read. Open it in Google Sheets, use File → Save as Google Sheets, and connect the "+ "converted sheet instead.",+ )+ return (+ False,+ "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 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.pyindex 222ffba4ad17..11ca5841169e 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@@ -494,3 +494,31 @@ def test_google_sheets_client_sets_request_timeout():assert client.http_client.timeout == _REQUEST_TIMEOUT_SECONDSassert client.http_client.timeout is not None+++ "api_message,expected_fragment",+ [+ # The reported case: an uploaded .xlsx the Sheets API refuses to open.+ (+ "This operation is not supported for this document. The document must not be an Office file.",+ "Save as Google Sheets",+ ),+ # Any other 400 from the Sheets API falls back to a clean, actionable message.+ ("Some other bad request", "shared with our service account"),+ ],+)+def test_validate_credentials_maps_api_error_to_friendly_message(api_message, expected_fragment):+ 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 = _api_error(+ 400, message=api_message, status="INVALID_ARGUMENT"+ )+ 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+ 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 "")