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(warehouse): return 400 when source create schema discovery raises a known credential error (#77030)

Tom Owersd5a04a8375df
diff --git a/products/warehouse_sources/backend/presentation/views/external_data_source.py b/products/warehouse_sources/backend/presentation/views/external_data_source.py
index ea33f5ece27d..f287559681c1 100644
--- a/products/warehouse_sources/backend/presentation/views/external_data_source.py
+++ b/products/warehouse_sources/backend/presentation/views/external_data_source.py
@@ -2213,6 +2213,27 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixi
status=status.HTTP_400_BAD_REQUEST,
data={"message": f"Source type '{source_type}' does not support schema discovery."},
)
+ except Exception as e:
+ # `get_schemas` opens its own connection, so credentials validated above can still fail
+ # here (e.g. a BigQuery service account key rotated/revoked in between). Classify via
+ # the source's own non-retryable-error map, same as `database_schema` and
+ # `refresh_schemas`, and roll back the row so a source that can't discover its schema
+ # doesn't linger half-created.
+ error_message, is_expected_source_error = _classify_refresh_schemas_error(source, e)
+ if not is_expected_source_error:
+ capture_exception(
+ e,
+ {
+ "source_type": source_type,
+ "team_id": self.team_id,
+ "source_id": str(new_source_model.id),
+ },
+ )
+ new_source_model.delete()
+ return Response(
+ status=status.HTTP_400_BAD_REQUEST,
+ data={"message": error_message},
+ )
if is_direct_query:
new_source_model.connection_metadata = get_direct_connection_metadata(
source_impl=source,
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.py
index 3c1a084cec14..3b25b50c7d46 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
@@ -2063,6 +2063,60 @@ class TestExternalDataSource(APIBaseTest):
assert "'private_key'" in response.json()["message"]
assert "'private_key_id'" in response.json()["message"]
+ @patch("products.warehouse_sources.backend.presentation.views.external_data_source.capture_exception")
+ def test_create_external_data_source_bigquery_returns_400_on_credentials_rejected_during_schema_discovery(
+ self, mock_capture_exception
+ ):
+ # `get_schemas` opens its own BigQuery connection, so credentials that passed the earlier
+ # live validation can still be rejected here (e.g. a key rotated/revoked in between).
+ from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery import (
+ BIGQUERY_CREDENTIALS_REJECTED_ERROR,
+ BigQueryCredentialsRejectedError,
+ )
+
+ with (
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.source.BigQuerySource.validate_credentials",
+ return_value=(True, None),
+ ),
+ patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.source.BigQuerySource.get_schemas",
+ # Mirrors the real message `get_columns` raises: it keeps the `invalid_grant` marker
+ # inline so `get_non_retryable_errors` ("invalid_grant" -> BIGQUERY_CREDENTIALS_REJECTED_ERROR)
+ # still matches it, unlike the plain BIGQUERY_CREDENTIALS_REJECTED_ERROR constant.
+ side_effect=BigQueryCredentialsRejectedError(
+ "Your BigQuery service account credentials were rejected by Google (invalid_grant). "
+ "The key may have been rotated or revoked, or the service account deleted. "
+ "Please upload a new Google Cloud JSON key file."
+ ),
+ ),
+ ):
+ response = self.client.post(
+ f"/api/environments/{self.team.pk}/external_data_sources/",
+ data={
+ "source_type": "BigQuery",
+ "created_via": "web",
+ "payload": {
+ "schemas": [
+ {"name": "my_table", "should_sync": True, "sync_type": "full_refresh"},
+ ],
+ "dataset_id": "my_project.my_dataset",
+ "key_file": {
+ "project_id": "my_project",
+ "private_key": "my_private_key",
+ "private_key_id": "my_private_key_id",
+ "token_uri": "https://google.com",
+ "client_email": "[email protected]",
+ },
+ },
+ },
+ )
+
+ assert response.status_code == 400
+ assert response.json()["message"] == BIGQUERY_CREDENTIALS_REJECTED_ERROR
+ assert ExternalDataSource.objects.count() == 0
+ mock_capture_exception.assert_not_called()
+
def test_list_external_data_source_query_count_does_not_scale_with_sources(self):
# N+1 regression guard: created_by, revenue_analytics_config, schemas and the latest job are
# all fetched up front, so listing must cost the same number of queries regardless of how many