: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(experiments): Handle deleted experiment in exposures query runner (#96129)
Juraj Majerikf3bff4e040e2
diff --git a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.pyindex d505c1facc74..8a17c4bd0efc 100644--- a/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py+++ b/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py@@ -72,7 +72,10 @@ class ExperimentExposuresQueryRunner(QueryRunner):raise ValidationError("feature_flag key is required")self.exposure_criteria = self.query.exposure_criteria- self.experiment = Experiment.objects.get(id=self.query.experiment_id, team=self.team)+ try:+ self.experiment = Experiment.objects.get(id=self.query.experiment_id, team=self.team)+ except Experiment.DoesNotExist:+ raise ValidationError(f"Experiment with id {self.query.experiment_id} not found")self.feature_flag_key: str = self.experiment.feature_flag.key_without_tombstone()# From the DB flag, not the query dict — callers vary in the feature_flag shape they# pass (full flag object vs bare filters), and a missed group index would bypass thediff --git a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.pyindex 1405c793c5b3..ff70ba93086b 100644--- a/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py+++ b/products/experiments/backend/hogql_queries/test/experiment_query_runner/test_experiment_exposures_query_runner.py@@ -75,6 +75,17 @@ class TestExperimentExposuresQueryRunner(ExperimentQueryRunnerBaseTest):self.assertIn("has no variants", str(ctx.exception))+ def test_missing_experiment_raises_validation_error(self):+ # Cached exposure queries can outlive their experiment; a stale id must+ # surface as a validation error, not an uncaught Experiment.DoesNotExist.+ query = self._null_multivariate_query()+ query.experiment_id = self.experiment.id + 1000++ with self.assertRaises(ValidationError) as ctx:+ ExperimentExposuresQueryRunner(team=self.team, query=query)++ self.assertIn("not found", str(ctx.exception))+@freeze_time("2024-01-07T12:00:00Z")def test_exposure_query_resolves_soft_deleted_feature_flag_key(self):# Exposure events are captured under the flag's original key.