diff --git a/src/sentry/rules/history/endpoints/project_rule_stats.py b/src/sentry/rules/history/endpoints/project_rule_stats.py index 9fc28a081e3ecb..47a2f0883fc14a 100644 --- a/src/sentry/rules/history/endpoints/project_rule_stats.py +++ b/src/sentry/rules/history/endpoints/project_rule_stats.py @@ -5,6 +5,7 @@ from typing import Any, TypedDict from drf_spectacular.utils import extend_schema +from rest_framework.exceptions import ParseError from rest_framework.request import Request from rest_framework.response import Response @@ -15,6 +16,7 @@ from sentry.api.utils import get_date_range_from_params from sentry.apidocs.constants import RESPONSE_FORBIDDEN, RESPONSE_NOT_FOUND, RESPONSE_UNAUTHORIZED from sentry.apidocs.parameters import GlobalParams, IssueAlertParams +from sentry.exceptions import InvalidParams from sentry.models.project import Project from sentry.models.rule import Rule from sentry.rules.history import fetch_rule_hourly_stats @@ -65,6 +67,9 @@ def get(self, request: Request, project: Project, rule: Rule | Workflow) -> Resp """ Note that results are returned in hourly buckets. """ - start, end = get_date_range_from_params(request.GET) + try: + start, end = get_date_range_from_params(request.GET) + except InvalidParams as e: + raise ParseError(detail=str(e)) results = fetch_rule_hourly_stats(rule, start, end) return Response(serialize(results, request.user, TimeSeriesValueSerializer())) diff --git a/tests/sentry/rules/history/endpoints/test_project_rule_stats.py b/tests/sentry/rules/history/endpoints/test_project_rule_stats.py index fbdfd36e35bfa0..dbda9ddb71bac2 100644 --- a/tests/sentry/rules/history/endpoints/test_project_rule_stats.py +++ b/tests/sentry/rules/history/endpoints/test_project_rule_stats.py @@ -74,3 +74,14 @@ def test(self) -> None: {"date": now - timedelta(hours=1), "count": 1}, {"date": now, "count": 0}, ] + + def test_invalid_date_range(self) -> None: + rule = self.create_project_rule(project=self.event.project) + self.login_as(self.user) + self.get_error_response( + self.organization.slug, + self.project.slug, + rule.id, + start="invalid", + status_code=400, + )