: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(insights): don't report user-safe async query errors to error tracking (#68023)
posthog[bot]a5feb7c9a5d9
diff --git a/posthog/clickhouse/client/execute_async.py b/posthog/clickhouse/client/execute_async.pyindex ae1ed6aca774..352e7f8afbfb 100644--- a/posthog/clickhouse/client/execute_async.py+++ b/posthog/clickhouse/client/execute_async.py@@ -243,14 +243,17 @@ def execute_process_query(from posthog.rbac.user_access_control import UserAccessControlErrorquery_status.results = None # Clear results in case they are faulty- if (- isinstance(err, APIException | ExposedHogQLError | ExposedCHQueryError | UserAccessControlError)- or is_staff_user- ):+ is_user_safe_error = isinstance(+ err, APIException | ExposedHogQLError | ExposedCHQueryError | UserAccessControlError+ )+ if is_user_safe_error or is_staff_user:# We can only expose the error message if it's a known safe error OR if the user is PostHog staffquery_status.error_message = str(err)logger.exception("Error processing query async", team_id=team_id, query_id=query_id, exc_info=True)- capture_exception(err)+ if not is_user_safe_error:+ # User-safe errors (e.g. a malformed HogQL query) are already returned to the user as a 400,+ # so don't report them to error tracking — only genuine server-side failures belong there.+ capture_exception(err)# Do not raise here, the task itself did its job and we cannot recoverfinally:query_status.end_time = datetime.datetime.now(datetime.UTC)diff --git a/posthog/clickhouse/client/test/test_execute_async.py b/posthog/clickhouse/client/test/test_execute_async.pyindex 20ee32bd52e0..82d3914179a0 100644--- a/posthog/clickhouse/client/test/test_execute_async.py+++ b/posthog/clickhouse/client/test/test_execute_async.py@@ -13,6 +13,7 @@ from parameterized import parameterizedfrom posthog.schema import ClickhouseQueryProgress, QueryStatusfrom posthog.hogql.constants import DEFAULT_POSTHOG_AI_RETURNED_ROWS+from posthog.hogql.errors import ExposedHogQLErrorfrom posthog.clickhouse.client import (execute_async as client,@@ -21,7 +22,7 @@ from posthog.clickhouse.client import (from posthog.clickhouse.client.async_task_chain import task_chain_contextfrom posthog.clickhouse.client.execute_async import QueryNotFoundError, QueryStatusManager, execute_process_queryfrom posthog.clickhouse.query_tagging import tag_queries-from posthog.errors import CHQueryErrorTooManySimultaneousQueries+from posthog.errors import CHQueryErrorTooManySimultaneousQueries, ExposedCHQueryErrorfrom posthog.models import Organization, Teamfrom posthog.models.user import Userfrom posthog.redis import get_client@@ -136,6 +137,30 @@ class TestExecuteProcessQuery(TestCase):args_loaded = json.loads(args[1])self.assertEqual(args_loaded["results"], [None, None, None, 1.0, "👍"])+ @parameterized.expand(+ [+ ("user_safe_ch_error", ExposedCHQueryError("NOT_AN_AGGREGATE"), False),+ ("user_safe_hogql_error", ExposedHogQLError("bad query"), False),+ ("server_error", ValueError("something broke"), True),+ ]+ )+ @patch("posthog.clickhouse.client.execute_async.capture_exception")+ @patch("posthog.clickhouse.client.execute_async.redis.get_client")+ @patch("posthog.api.services.query.process_query_dict")+ def test_execute_process_query_only_captures_non_user_safe_errors(+ self, _name, error, should_capture, mock_process_query_dict, mock_redis_client, mock_capture_exception+ ):+ mock_redis = MagicMock()+ mock_redis.get.return_value = json.dumps(+ {"id": self.query_id, "team_id": self.team.id, "complete": False, "error": False}+ ).encode()+ mock_redis_client.return_value = mock_redis+ mock_process_query_dict.side_effect = error++ execute_process_query(self.team.id, self.user.id, self.query_id, self.query_json, self.limit_context)++ self.assertEqual(mock_capture_exception.called, should_capture)+class ClickhouseClientTestCase(TestCase, ClickhouseTestMixin):def setUp(self):