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(clickhouse): recognize a bare HTTP 400 as a non-ClickHouse response (#98939)

Tom Owerseb9e4c7a6152
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.py
index 42f22bd0ac7d..8290db3e69d4 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
@@ -111,6 +111,13 @@ ClickHouseErrors: dict[str, str] = {
# 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).",
+ # clickhouse-connect's `_error_handler` only produces this wording ("returned response
+ # code" rather than "received ClickHouse error code") when the response carries no
+ # `X-ClickHouse-Exception-Code` header — a genuine ClickHouse query error always sets
+ # that header, so a bare 400 means something in front of ClickHouse (a proxy, WAF, or
+ # tunnel) rejected the request before it reached the server. Same cause as 404, different
+ # status code some proxies use instead.
+ "returned response code 400": "We reached your ClickHouse host but it returned a 400, 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).",
# `_get_client` raises this when the host answers 2xx with a body that isn't a
# ClickHouse response (a proxy/LB page, or a different service on the host/port).
"did not return a valid clickhouse response": NOT_A_CLICKHOUSE_HTTP_RESPONSE,
@@ -283,6 +290,12 @@ class ClickHouseSource(SimpleSource[ClickHouseSourceConfig], SSHTunnelMixin, Val
# answers queries with 404, so retrying can't recover. We match only
# 404, not transient gateway codes (502/503/504), which stay retryable.
"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).",
+ # Same cause as the 404 above: clickhouse-connect only wraps a response as "returned
+ # response code N" (rather than "received ClickHouse error code N") when it carries no
+ # `X-ClickHouse-Exception-Code` header, which a genuine ClickHouse query error always
+ # sets. A bare 400 means a proxy, WAF, or tunnel in front of ClickHouse rejected the
+ # request before it reached the server, so retrying replays the identical failure.
+ "returned response code 400": "We reached your ClickHouse host but it returned a 400, 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).",
# `_get_client` wraps the driver's construction-time probe failure ("too many
# values to unpack") into this message when the host answers 2xx with a body that
# isn't a ClickHouse response. The endpoint isn't serving the ClickHouse HTTP
diff --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.py
index e39789b93019..96783e8f0340 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
@@ -652,6 +652,9 @@ class TestClickHouseSourceNonRetryableErrors:
"Connection refused",
"certificate verify failed",
"HTTPDriver for https://example.ngrok-free.dev:443 returned response code 404",
+ # A bare 400 (no "received ClickHouse error code" wording) means a proxy/tunnel in
+ # front of ClickHouse rejected the request, not the server itself — same cause as 404.
+ "HTTPDriver for https://example.ngrok-free.dev:443 returned response code 400",
# MEMORY_LIMIT_EXCEEDED (code 241) — server-wide OvercommitTracker kill
"HTTPDriver for https://host:8443 received ClickHouse error code 241\n Code: 241. "
"DB::Exception: (total) memory limit exceeded: would use 108.01 GiB, maximum: 108.00 GiB. "
@@ -1032,6 +1035,15 @@ class TestTranslateError:
assert translated is not None
assert "404" in translated
+ def test_400_maps_to_wrong_interface_message(self):
+ # A proxy/tunnel in front of ClickHouse rejecting the request (no ClickHouse error
+ # header) answers with a bare 400; 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 400"
+ translated = ClickHouseSource._translate_error(msg)
+ assert translated is not None
+ assert "400" in translated
+
def test_non_clickhouse_response_maps_to_actionable_message(self):
# The wrapped probe error must surface the actionable "not serving ClickHouse"
# message during onboarding, not fall through to the generic one.