: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(query): stop capturing DRF validation errors from query runners (#100479)
Juraj Majerike466af9508b8
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.pyindex c456a0a8a62f..7d850d1adec1 100644--- a/posthog/hogql_queries/query_runner.py+++ b/posthog/hogql_queries/query_runner.py@@ -15,6 +15,7 @@ import structlogimport posthoganalyticsfrom prometheus_client import Counter, Histogramfrom pydantic import BaseModel, ConfigDict+from rest_framework.exceptions import ValidationError as DRFValidationErrorfrom posthog.schema import (AccountsQuery,@@ -339,10 +340,17 @@ def _classify_error_for_slo(exc: Exception) -> tuple[QueryErrorCategory, SloOutc(EstimatedQueryExecutionTimeTooLong, QuerySizeExceeded) are a minorityworth living with for now.- UserAccessControlError is folded into USER_ERROR locally since- classify_query_error doesn't recognise it but a 403 is the user's input,- not a service failure.+ UserAccessControlError and DRF ValidationError are folded into USER_ERROR+ locally since classify_query_error doesn't recognise them, but a 403 or a+ 400 is the user's input, not a service failure. A ValidationError with an+ explicit cause is a technical error a runner converted for display (e.g.+ the experiments error handler wrapping a ClickHouse OOM) — classify the+ original so real platform failures keep failing the SLO."""+ if isinstance(exc, DRFValidationError):+ if isinstance(exc.__cause__, Exception):+ return _classify_error_for_slo(exc.__cause__)+ return QueryErrorCategory.USER_ERROR, SloOutcome.SUCCESSif isinstance(exc, UserAccessControlError):return QueryErrorCategory.USER_ERROR, SloOutcome.SUCCESSif isinstance(exc, APIQueriesBudgetExceeded):diff --git a/posthog/hogql_queries/test/test_query_runner.py b/posthog/hogql_queries/test/test_query_runner.pyindex 647931b062b3..2a7e6b7b7e80 100644--- a/posthog/hogql_queries/test/test_query_runner.py+++ b/posthog/hogql_queries/test/test_query_runner.py@@ -153,6 +153,11 @@ def setup_test_query_runner_class(base: type[QueryRunner] = QueryRunner):return TestQueryRunner+def _chain(exc: Exception, cause: Exception) -> Exception:+ exc.__cause__ = cause+ return exc++class TestQueryRunner(BaseTest):maxDiff = None@@ -894,6 +899,28 @@ class TestQueryRunner(BaseTest):"error",True,),+ (+ # Runner-raised user-facing validation (e.g. an experiment metric with no+ # exposures for the control variant yet) — rendered as a 400, must not+ # reach error tracking.+ "drf_validation_error",+ lambda: ValidationError("No exposures for the 'control' variant yet.", code="no_data"),+ SloOutcome.SUCCESS,+ "user_error",+ False,+ ),+ (+ # A technical error a runner converted to a ValidationError for display+ # (chained via `raise ... from`) — must keep failing the SLO and stay captured.+ "validation_error_wrapping_technical_error",+ lambda: _chain(+ ValidationError("This experiment query is using too much memory.", code="memory_limit_exceeded"),+ ClickHouseQueryMemoryLimitExceeded(),+ ),+ SloOutcome.FAILURE,+ "query_performance_error",+ True,+ ),("unclassified_value_error", ValueError, SloOutcome.FAILURE, "error", True),])diff --git a/products/experiments/backend/hogql_queries/error_handling.py b/products/experiments/backend/hogql_queries/error_handling.pyindex 5ed7b6147f14..160171cbd4a6 100644--- a/products/experiments/backend/hogql_queries/error_handling.py+++ b/products/experiments/backend/hogql_queries/error_handling.py@@ -312,8 +312,9 @@ def experiment_error_handler(method: F) -> F:if user_message is None:raise- # Get error code if available+ # Get error code if available. Chain the original explicitly: the query SLO+ # classifier reads __cause__ to keep converted technical errors counted as failures.error_code = ERROR_TYPE_TO_CODE.get(type(e))- raise ValidationError(user_message, code=error_code)+ raise ValidationError(user_message, code=error_code) from ereturn cast(F, wrapper)