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(replay): stop masking recordings-list errors as generic 500 (#68451)

posthog[bot]37a3df4a751e
diff --git a/posthog/session_recordings/session_recording_api.py b/posthog/session_recordings/session_recording_api.py
index 6f9b9d7acf95..077f3ced94be 100644
--- a/posthog/session_recordings/session_recording_api.py
+++ b/posthog/session_recordings/session_recording_api.py
@@ -56,6 +56,8 @@ from posthog.schema import (
RecordingsQuery,
)
+from posthog.hogql.errors import ExposedHogQLError
+
from posthog.api.person import MinimalPersonSerializer
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.streaming import sse_streaming_response
@@ -70,7 +72,7 @@ from posthog.auth import (
)
from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
from posthog.cloud_utils import is_cloud
-from posthog.errors import CHQueryErrorCannotScheduleTask, CHQueryErrorTooManySimultaneousQueries
+from posthog.errors import CHQueryErrorCannotScheduleTask, CHQueryErrorTooManySimultaneousQueries, ExposedCHQueryError
from posthog.event_usage import report_user_action
from posthog.exceptions_capture import capture_exception
from posthog.helpers.impersonation import is_impersonated
@@ -912,10 +914,17 @@ class SessionRecordingViewSet(
except CHQueryErrorTooManySimultaneousQueries:
SESSION_RECORDING_THROTTLED.labels(location="too_many_simultaneous_queries", auth_type=auth_type).inc()
raise Throttled(detail="Too many simultaneous queries. Try again later.")
+ except (ExposedHogQLError, ExposedCHQueryError) as e:
+ # A bad filter or query (e.g. a property referencing a field that doesn't exist on the
+ # event) is the caller's problem, not a server error. Surface the actual reason as a 400
+ # instead of collapsing it into a generic 500.
+ raise exceptions.ValidationError(str(e), getattr(e, "code_name", None))
+ except exceptions.APIException:
+ # ValidationError, Throttled, and the ClickHouse capacity / timeout / memory-limit
+ # exceptions already carry a correct status code and a user-safe message. Let DRF render
+ # them as-is rather than masking a well-formed response behind a generic 500.
+ raise
except (ServerException, Exception) as e:
- if isinstance(e, exceptions.ValidationError):
- raise
-
if isinstance(e, ServerException) and "CHQueryErrorTimeoutExceeded" in str(e):
SESSION_RECORDING_THROTTLED.labels(location="query_timeout_exceeded", auth_type=auth_type).inc()
raise Throttled(detail="Query timeout exceeded. Try again later.")
diff --git a/posthog/session_recordings/test/test_session_recordings.py b/posthog/session_recordings/test/test_session_recordings.py
index dc1c9df5b1e1..2fefc40b8760 100644
--- a/posthog/session_recordings/test/test_session_recordings.py
+++ b/posthog/session_recordings/test/test_session_recordings.py
@@ -27,7 +27,10 @@ from rest_framework import status
from posthog.schema import LogEntryPropertyFilter, RecordingsQuery
+from posthog.hogql.errors import QueryError
+
from posthog.errors import CHQueryErrorCannotScheduleTask, CHQueryErrorTooManySimultaneousQueries
+from posthog.exceptions import ClickHouseQueryMemoryLimitExceeded
from posthog.models import Organization, SessionRecording, User
from posthog.models.team import Team
from posthog.models.utils import uuid7
@@ -1350,6 +1353,32 @@ class TestSessionRecordings(APIBaseTest, ClickhouseTestMixin, QueryMatchingTest)
"type": "throttled_error",
}
+ @parameterized.expand(
+ [
+ (
+ "hogql_query_error",
+ QueryError("Field not found: $device_type"),
+ status.HTTP_400_BAD_REQUEST,
+ "Field not found: $device_type",
+ ),
+ (
+ "clickhouse_memory_limit",
+ ClickHouseQueryMemoryLimitExceeded(),
+ 504,
+ "max memory limit",
+ ),
+ ]
+ )
+ @patch("posthog.session_recordings.queries.session_recording_list_from_query.SessionRecordingListFromQuery.run")
+ def test_session_recordings_surfaces_real_error(
+ self, _name, exception, expected_status, expected_detail_substring, mock_run
+ ):
+ mock_run.side_effect = exception
+ response = self.client.get(f"/api/projects/{self.team.id}/session_recordings")
+ assert response.status_code == expected_status
+ # the real reason must reach the client, not a generic "internal server error"
+ assert expected_detail_substring in response.json()["detail"]
+
def test_sync_execute_ch_cannot_schedule_task_retry_then_503(self):
"""Test that list_blocks throws CHQueryErrorCannotScheduleTask multiple times and eventually returns 503"""
call_count = 0