Browse Source

fix(discover): Handle invalid arithmetic (#35890)

- Need to catch ArithmeticError here too so we don't 500 on invalid
  equations
- fixes SENTRY-VBR
William Mak 2 years ago
parent
commit
3b8229cbc4

+ 2 - 2
src/sentry/discover/endpoints/serializers.py

@@ -9,7 +9,7 @@ from sentry.api.fields.empty_integer import EmptyIntegerField
 from sentry.api.serializers.rest_framework import ListField
 from sentry.api.utils import InvalidParams, get_date_range_from_params
 from sentry.constants import ALL_ACCESS_PROJECTS
-from sentry.discover.arithmetic import categorize_columns
+from sentry.discover.arithmetic import ArithmeticError, categorize_columns
 from sentry.discover.models import MAX_TEAM_KEY_TRANSACTIONS, TeamKeyTransaction
 from sentry.exceptions import InvalidSearchQuery
 from sentry.models import Team
@@ -229,7 +229,7 @@ class DiscoverSavedQuerySerializer(serializers.Serializer):
                     orderby=query.get("orderby"),
                 )
                 builder.get_snql_query().validate()
-            except InvalidSearchQuery as err:
+            except (InvalidSearchQuery, ArithmeticError) as err:
                 raise serializers.ValidationError(f"Cannot save invalid query: {err}")
 
         return {

+ 22 - 0
tests/snuba/api/endpoints/test_discover_saved_queries.py

@@ -598,6 +598,28 @@ class DiscoverSavedQueriesVersion2Test(DiscoverSavedQueryBase):
         assert response.status_code == 201, response.content
         assert DiscoverSavedQuery.objects.filter(name="Equation query").exists()
 
+    def test_save_with_invalid_equation(self):
+        with self.feature(self.feature_name):
+            response = self.client.post(
+                self.url,
+                {
+                    "name": "Equation query",
+                    "projects": [-1],
+                    "fields": [
+                        "title",
+                        "equation|count_if(measurements.lcp,greater,4000) / 0",
+                        "count()",
+                        "count_if(measurements.lcp,greater,4000)",
+                    ],
+                    "orderby": "equation[0]",
+                    "range": "24h",
+                    "query": "title:1",
+                    "version": 2,
+                },
+            )
+        assert response.status_code == 400, response.content
+        assert not DiscoverSavedQuery.objects.filter(name="Equation query").exists()
+
     def test_save_invalid_query(self):
         with self.feature(self.feature_name):
             response = self.client.post(