: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(clickhouse): surface actionable errors on source setup failures (#72214)
Tom Owersdd4d5ccf5884
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.pyindex 747aa8fa5ac6..e25ab1011e7c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/source.py@@ -46,6 +46,22 @@ from products.warehouse_sources.backend.types import ExternalDataSourceType, Incif TYPE_CHECKING:from products.warehouse_sources.backend.models.external_data_source import ExternalDataSource+# Shown when we can't map the failure to a specific cause. Names the usual+# culprits so the user has something concrete to check, rather than a bare+# "check your details".+GENERIC_CONNECTION_ERROR = (+ "Could not connect to ClickHouse. Check that the host and port are correct, the HTTPS setting matches "+ "your server, and that PostHog's IP addresses are allowed through any firewall."+)++# A transient gateway/rate-limit response that survived the in-process connect+# retries. The connection details are fine — the server is momentarily busy or+# waking (idle ClickHouse Cloud services routinely 5xx the first request), so+# ask the user to retry rather than implying their credentials are wrong.+_TEMPORARILY_UNAVAILABLE = (+ "ClickHouse is temporarily busy or unavailable and didn't accept the connection. Wait a moment and try again."+)+# Error message → user-friendly translation. Matched as a substring of the# exception string. Patterns are lowercase-matched.ClickHouseErrors: dict[str, str] = {@@ -60,6 +76,14 @@ ClickHouseErrors: dict[str, str] = {"connection refused": "Could not connect to ClickHouse on the given host/port","connection timed out": "Connection to ClickHouse timed out. Does your database have our IP addresses allow-listed?","ssl": "TLS/SSL handshake failed. If your server does not use TLS, disable the HTTPS toggle.",+ # The host answered but isn't serving the ClickHouse HTTP interface on this+ # host/port (wrong port, a proxy, or a native-protocol port). Same wording+ # as the sync-time non-retryable handling.+ "returned response code 404": "We reached your ClickHouse host but it returned a 404, so it isn't serving the ClickHouse HTTP interface on that host/port. Please check the host, port, and HTTPS setting (and any tunnel or proxy in front of it).",+ "returned response code 429": _TEMPORARILY_UNAVAILABLE,+ "returned response code 502": _TEMPORARILY_UNAVAILABLE,+ "returned response code 503": _TEMPORARILY_UNAVAILABLE,+ "returned response code 504": _TEMPORARILY_UNAVAILABLE,}@@ -336,16 +360,16 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val)except ClickHouseConnectionError as e:message = self._translate_error(str(e))- return False, message or "Could not connect to ClickHouse. Please check all connection details are valid."+ return False, message or GENERIC_CONNECTION_ERRORexcept (DatabaseError, OperationalError, ClickHouseError) as e:message = self._translate_error(str(e))if message is None:capture_exception(e)- return False, "Could not connect to ClickHouse. Please check all connection details are valid."+ return False, GENERIC_CONNECTION_ERRORreturn False, messageexcept Exception as e:capture_exception(e)- return False, "Could not connect to ClickHouse. Please check all connection details are valid."+ return False, GENERIC_CONNECTION_ERRORreturn True, Nonediff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.pyindex 67f94cc0bb8b..23c466f0c796 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clickhouse/test_clickhouse.py@@ -29,7 +29,11 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.clickhousefilter_clickhouse_incremental_fields,get_primary_keys_for_schemas,)-from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse.source import ClickHouseSource+from products.warehouse_sources.backend.temporal.data_imports.sources.clickhouse.source import (+ _TEMPORARILY_UNAVAILABLE,+ GENERIC_CONNECTION_ERROR,+ ClickHouseSource,+)from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import SourceSchemafrom products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.predicates import (ColumnTypeCategory,@@ -854,6 +858,22 @@ class TestTranslateError:def test_returns_none_for_unrecognised_error(self):assert ClickHouseSource._translate_error("Some random error") is None+ def test_404_maps_to_wrong_interface_message(self):+ # A wrong port/proxy answers with 404; the message must name that cause+ # rather than fall through to the generic "check your details".+ msg = "HTTPDriver for https://host:8443 returned response code 404"+ translated = ClickHouseSource._translate_error(msg)+ assert translated is not None+ assert "404" in translated+ assert translated != GENERIC_CONNECTION_ERROR++ @pytest.mark.parametrize("code", ["429", "502", "503", "504"])+ def test_transient_gateway_responses_ask_to_retry(self, code):+ # A busy/waking server (survived connect retries) must not be reported as+ # bad credentials — it should tell the user to retry.+ msg = f"HTTPDriver for https://host:8443 returned response code {code}"+ assert ClickHouseSource._translate_error(msg) == _TEMPORARILY_UNAVAILABLE+class TestGetSchemas:"""Tests `get_schemas` with a fully mocked ClickHouse client."""@@ -969,7 +989,7 @@ class TestSourceClassValidateCredentials:valid, msg = source.validate_credentials(config, team_id=1)assert valid is False- assert msg == "Could not connect to ClickHouse. Please check all connection details are valid."+ assert msg == GENERIC_CONNECTION_ERRORclass TestHasDuplicatePrimaryKeys: