Skip to content

Commit

Permalink
refactor: move the ClickHouse and HogQL query exception handling to t…
Browse files Browse the repository at this point in the history
…he SessionRecordingViewSet level
  • Loading branch information
namit-chandwani committed Aug 21, 2024
1 parent 111940f commit f19d4f0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
15 changes: 5 additions & 10 deletions posthog/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,10 @@ class CHQueryErrorTooManySimultaneousQueries(InternalCHQueryError):
pass


class CHQueryErrorTypeMismatch(ExposedCHQueryError):
pass


CLICKHOUSE_SPECIFIC_ERROR_LOOKUP = {
"TOO_MANY_SIMULTANEOUS_QUERIES": CHQueryErrorTooManySimultaneousQueries(
"Too many simultaneous queries. Try again later.", code=202
),
"TYPE_MISMATCH": CHQueryErrorTypeMismatch(
"One or more property values in your filters are in an incorrect format."
" Please check and adjust them to match the required data types.",
code=53,
),
}


Expand Down Expand Up @@ -161,7 +152,11 @@ class CHQueryErrorTypeMismatch(ExposedCHQueryError):
50: ErrorCodeMeta("UNKNOWN_TYPE"),
51: ErrorCodeMeta("EMPTY_LIST_OF_COLUMNS_QUERIED"),
52: ErrorCodeMeta("COLUMN_QUERIED_MORE_THAN_ONCE"),
53: ErrorCodeMeta("TYPE_MISMATCH", user_safe=True),
53: ErrorCodeMeta(
"TYPE_MISMATCH",
user_safe="One or more property values in your filters are in an incorrect format."
" Please check and adjust them to match the required data types.",
),
55: ErrorCodeMeta("STORAGE_REQUIRES_PARAMETER"),
56: ErrorCodeMeta("UNKNOWN_STORAGE"),
57: ErrorCodeMeta("TABLE_ALREADY_EXISTS"),
Expand Down
4 changes: 0 additions & 4 deletions posthog/hogql/query.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import dataclasses
from typing import Optional, Union, cast

from rest_framework.exceptions import ValidationError

from posthog.clickhouse.client.connection import Workload
from posthog.errors import ExposedCHQueryError
from posthog.hogql import ast
Expand Down Expand Up @@ -202,8 +200,6 @@ def execute_hogql_query(
team_id=team.pk,
readonly=True,
)
except (ExposedHogQLError, ExposedCHQueryError) as e:
raise ValidationError(str(e), getattr(e, "code_name", None))
except Exception as e:
if debug:
results = []
Expand Down
8 changes: 7 additions & 1 deletion posthog/session_recordings/session_recording_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.utils.encoders import JSONEncoder
from rest_framework.exceptions import ValidationError

from posthog.api.person import MinimalPersonSerializer
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import safe_clickhouse_string
from posthog.auth import SharingAccessTokenAuthentication
from posthog.cloud_utils import is_cloud
from posthog.constants import SESSION_RECORDINGS_FILTER_IDS
from posthog.errors import ExposedCHQueryError
from posthog.hogql.errors import ExposedHogQLError
from posthog.models import User, Team
from posthog.models.filters.session_recordings_filter import SessionRecordingsFilter
from posthog.models.person.person import PersonDistinctId
Expand Down Expand Up @@ -276,7 +279,10 @@ def safely_get_object(self, queryset) -> SessionRecording:

def list(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
filter = SessionRecordingsFilter(request=request, team=self.team)
return list_recordings_response(filter, request, self.get_serializer_context())
try:
return list_recordings_response(filter, request, self.get_serializer_context())
except (ExposedHogQLError, ExposedCHQueryError) as e:
raise ValidationError(str(e), getattr(e, "code_name", None))

@extend_schema(
description="""
Expand Down

0 comments on commit f19d4f0

Please sign in to comment.