: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(data-warehouse): show friendly connection error during schema discovery (#66705)
Tom Owers6deab6e8de65
diff --git a/products/data_warehouse/backend/presentation/views/external_data_source.py b/products/data_warehouse/backend/presentation/views/external_data_source.pyindex 623766d14325..00777a7ab1e8 100644--- a/products/data_warehouse/backend/presentation/views/external_data_source.py+++ b/products/data_warehouse/backend/presentation/views/external_data_source.py@@ -2471,12 +2471,12 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixitry:schemas = source.get_schemas(source_config, self.team_id)except Exception as e:- _, is_expected_source_error = _classify_refresh_schemas_error(source, e)+ 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})return Response(status=status.HTTP_400_BAD_REQUEST,- data={"message": str(e)},+ data={"message": error_message},)# Best-effort per-endpoint scope probe — transient failure falls back to "available".@@ -2587,8 +2587,10 @@ class ExternalDataSourceViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixidata={"message": f"Source type '{source_type}' does not support one-shot setup."},)except Exception as e:- capture_exception(e, {"source_type": source_type, "team_id": self.team_id})- return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": str(e)})+ 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})+ return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": error_message})if not source_schemas:return Response(diff --git a/products/data_warehouse/backend/tests/api/test_external_data_source.py b/products/data_warehouse/backend/tests/api/test_external_data_source.pyindex 4ac16067a3dd..f86aacc925d9 100644--- a/products/data_warehouse/backend/tests/api/test_external_data_source.py+++ b/products/data_warehouse/backend/tests/api/test_external_data_source.py@@ -3881,6 +3881,57 @@ class TestExternalDataSource(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertIs(response.json()[0]["xmin_available"], expected_xmin_available)+ @parameterized.expand(+ [+ ("database_schema", "database_schema/", {"source_type": "Stripe"}),+ ("setup", "setup/", {"source_type": "Stripe", "payload": {}}),+ ]+ )+ @patch("products.data_warehouse.backend.presentation.views.external_data_source.capture_exception")+ @patch("products.data_warehouse.backend.presentation.views.external_data_source.SourceRegistry.get_source")+ def test_schema_discovery_returns_friendly_message_for_expected_error(+ self, _name, endpoint, body, mock_get_source, mock_capture_exception+ ):+ source = mock_get_source.return_value+ source.validate_config.return_value = (True, [])+ source.parse_config.return_value = Mock()+ source.validate_credentials.return_value = (True, None)+ source.get_non_retryable_errors.return_value = {}+ source.get_schemas.side_effect = Exception("connection timed out")++ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_sources/{endpoint}",+ data=body,+ )++ self.assertEqual(response.status_code, 400)+ self.assertEqual(+ response.json().get("message"),+ "Connection timed out while fetching schemas from the source.",+ )+ mock_capture_exception.assert_not_called()++ @patch("products.data_warehouse.backend.presentation.views.external_data_source.capture_exception")+ @patch("products.data_warehouse.backend.presentation.views.external_data_source.SourceRegistry.get_source")+ def test_setup_returns_generic_message_for_unexpected_error(self, mock_get_source, mock_capture_exception):+ source = mock_get_source.return_value+ source.validate_config.return_value = (True, [])+ source.parse_config.return_value = Mock()+ source.validate_credentials.return_value = (True, None)+ source.get_non_retryable_errors.return_value = {}+ raw_error = "psql: host=internal-db.prod user=admin password=hunter2 failed"+ source.get_schemas.side_effect = Exception(raw_error)++ response = self.client.post(+ f"/api/environments/{self.team.pk}/external_data_sources/setup/",+ data={"source_type": "Stripe", "payload": {}},+ )++ self.assertEqual(response.status_code, 400)+ # Unrecognized errors must not leak the raw exception text (e.g. credentials) to the client.+ self.assertEqual(response.json().get("message"), "Could not fetch schemas from source.")+ mock_capture_exception.assert_called_once()+def test_database_schema(self):postgres_connection = psycopg.connect(host=settings.PG_HOST,@@ -4034,10 +4085,13 @@ class TestExternalDataSource(APIBaseTest):)assert response.status_code == 400- assert response.json()["message"] == str(error)if expect_capture:+ # Unrecognized errors return the generic fallback (never the raw exception text) and are captured.+ assert response.json()["message"] == "Could not fetch schemas from source."mock_capture_exception.assert_called_once_with(error, {"source_type": "BigQuery", "team_id": self.team.pk})else:+ # Recognized errors surface their curated, user-safe message.+ assert response.json()["message"] == str(error)mock_capture_exception.assert_not_called()def test_database_schema_stripe_surfaces_per_endpoint_permission_errors(self):