Skip to content

Commit 557fa29

Browse files
authored
ref(metrics): catch assertionerror and invalidparams in metrics api (#72204)
1 parent 3aa1bef commit 557fa29

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/sentry/api/endpoints/organization_metrics_query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from sentry.api.base import region_silo_endpoint
1111
from sentry.api.bases import OrganizationEndpoint, OrganizationMetricsPermission
1212
from sentry.api.utils import get_date_range_from_params
13+
from sentry.exceptions import InvalidParams
1314
from sentry.sentry_metrics.querying.data import (
1415
MetricsAPIQueryResultsTransformer,
1516
MQLQuery,
@@ -176,6 +177,10 @@ def post(self, request: Request, organization) -> Response:
176177
).apply_transformer(MetricsAPIQueryResultsTransformer())
177178
except InvalidMetricsQueryError as e:
178179
return Response(status=400, data={"detail": str(e)})
180+
except InvalidParams as e:
181+
return Response(status=400, data={"detail": str(e)})
182+
except AssertionError as e:
183+
return Response(status=400, data={"detail": str(e)})
179184
except LatestReleaseNotFoundError as e:
180185
return Response(status=404, data={"detail": str(e)})
181186
except MetricsQueryExecutionError as e:

tests/sentry/api/endpoints/test_organization_metrics_query.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,33 @@ def test_recursion_error_query(self):
126126
"includeSeries": "false",
127127
},
128128
)
129+
130+
def test_formula_with_only_number(self):
131+
response = self.get_response(
132+
self.project.organization.slug,
133+
queries=[{"name": "query_1", "mql": "avg(d:transactions/duration@millisecond)"}],
134+
formulas=[{"mql": "100"}],
135+
qs_params={
136+
"statsPeriod": "3h",
137+
"interval": "1h",
138+
"project": [self.project.id],
139+
"environment": [],
140+
"includeSeries": "false",
141+
},
142+
)
143+
assert response.status_code == 400
144+
145+
def test_formula_with_number_equation_and_span_metric(self):
146+
response = self.get_response(
147+
self.project.organization.slug,
148+
queries=[{"name": "a", "mql": "avg(d:spans/webvital.inp@millisecond)"}],
149+
formulas=[{"mql": "$a"}, {"mql": "1 * 200"}],
150+
qs_params={
151+
"statsPeriod": "3h",
152+
"interval": "1h",
153+
"project": [self.project.id],
154+
"environment": [],
155+
"includeSeries": "false",
156+
},
157+
)
158+
assert response.status_code == 400

0 commit comments

Comments
 (0)