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(endpoints): return 4xx not 500 for bad materialize queries (#64729)

Jovan Sakovic4ad2114ab725
diff --git a/products/endpoints/backend/services/crud.py b/products/endpoints/backend/services/crud.py
index deca76ca342d..c6a08c8bca7d 100644
--- a/products/endpoints/backend/services/crud.py
+++ b/products/endpoints/backend/services/crud.py
@@ -240,6 +240,8 @@ class EndpointCrudService:
except APIException:
raise
+ except ValidationError:
+ raise
except Exception as e:
current_version = endpoint.get_version()
capture_exception(
diff --git a/products/endpoints/backend/services/materialization.py b/products/endpoints/backend/services/materialization.py
index 537c37629c35..4bcc920f9b0f 100644
--- a/products/endpoints/backend/services/materialization.py
+++ b/products/endpoints/backend/services/materialization.py
@@ -16,6 +16,7 @@ from rest_framework.request import Request
from posthog.hogql import ast
from posthog.hogql.context import HogQLContext
+from posthog.hogql.errors import ExposedHogQLError
from posthog.hogql.parser import parse_select
from posthog.hogql.printer import to_printed_hogql
from posthog.hogql.printer.utils import print_prepared_ast
@@ -148,6 +149,10 @@ class EndpointMaterializationService:
except ValidationError:
ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="enable", status="validation_error").inc()
raise
+ except ExposedHogQLError as e:
+ # A bad user query, not a system fault — surface as a 400.
+ ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="enable", status="validation_error").inc()
+ raise ValidationError(f"Cannot materialize endpoint. Reason: {e}")
except Exception:
ENDPOINT_MATERIALIZATION_EVENT_TOTAL.labels(action="enable", status="error").inc()
# Not a request-validation problem — surface as a server error, not a 400.
diff --git a/products/endpoints/backend/tests/test_endpoint_materialization.py b/products/endpoints/backend/tests/test_endpoint_materialization.py
index c7cbfbae4863..3681830127bf 100644
--- a/products/endpoints/backend/tests/test_endpoint_materialization.py
+++ b/products/endpoints/backend/tests/test_endpoint_materialization.py
@@ -17,6 +17,8 @@ from rest_framework.response import Response
from posthog.schema import RetentionQuery
+from posthog.hogql.errors import QueryError
+
from posthog.constants import RETENTION_FIRST_EVER_OCCURRENCE, TREND_FILTER_TYPE_EVENTS
from posthog.settings.temporal import DATA_MODELING_TASK_QUEUE
from posthog.sync import database_sync_to_async
@@ -325,6 +327,30 @@ class TestEndpointMaterialization(ClickhouseTestMixin, APIBaseTest):
# Should indicate variable metadata issue
self.assertIn("Cannot materialize endpoint", response.json()["detail"])
+ def test_enable_materialization_user_query_error_returns_400(self):
+ endpoint = create_endpoint_with_version(
+ name="test_hogql_error",
+ team=self.team,
+ query=self.sample_hogql_query,
+ created_by=self.user,
+ is_active=True,
+ )
+
+ with mock.patch.object(
+ EndpointMaterializationService,
+ "_enable_materialization_inner",
+ autospec=True,
+ side_effect=QueryError("Unresolved placeholder: {variables.event_id}"),
+ ):
+ response = self.client.patch(
+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/",
+ {"is_materialized": True, "data_freshness_seconds": 86400},
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())
+ self.assertIn("Unresolved placeholder", str(response.json()))
+
@parameterized.expand(
[
(