Skip to content

fix(alerts): return 400 instead of 500 when creating alert with unsupported dataset#119456

Draft
billyvg wants to merge 1 commit into
masterfrom
claude/serene-cannon-i7x8wh
Draft

fix(alerts): return 400 instead of 500 when creating alert with unsupported dataset#119456
billyvg wants to merge 1 commit into
masterfrom
claude/serene-cannon-i7x8wh

Conversation

@billyvg

@billyvg billyvg commented Jul 11, 2026

Copy link
Copy Markdown
Member

Summary

Fixes SENTRY-5PEFKeyError: <Dataset.Sessions: 'sessions'> in snuba_query_validator.py

Root cause: When a user POSTs to /api/0/organizations/{org}/alert-rules/ with dataset=sessions (or any dataset absent from query_datasets_to_type), the dict lookup on line 321 raises an unhandled KeyError that propagates as a 500.

Dataset.Sessions was removed from query_datasets_to_type (which now only contains Events, Transactions, PerformanceMetrics, Metrics, and EventsAnalyticsPlatform), but external API clients still send dataset=sessions. This has been happening since at least 2026-05-06 with 8 unique users affected.

Fix: Add an explicit dataset not in query_datasets_to_type check that raises a serializers.ValidationError (→ HTTP 400) with a message listing the valid datasets, before the dict lookup that was previously crashing.

Evidence:

  • Sentry issue: https://sentry.sentry.io/issues/SENTRY-5PEF
  • Stacktrace: sentry/snuba/snuba_query_validator.py:321 in _validate_queryquery_datasets_to_type[dataset] raises KeyError when dataset is Dataset.Sessions
  • The fix converts a 500 (unhandled exception) to a 400 (proper validation error)

Test plan

  • Added test_invalid_dataset_sessions_returns_validation_error to tests/sentry/snuba/test_validators.py covering the exact failing case
  • Existing SnubaQueryValidatorTest tests continue to pass

Claude session: https://claude.ai/code/session_01LjN9gb92hUE6jTqQ9dkjAm

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.


Generated by Claude Code

…ported dataset

When a user POSTs to /api/0/organizations/{org}/alert-rules/ with
`dataset=sessions` (or any dataset not in `query_datasets_to_type`),
the lookup `query_datasets_to_type[dataset]` raises a KeyError that
bubbles up as an unhandled 500. Add an explicit ValidationError before
the dict lookup so callers get a well-formed 400 with a helpful message
listing the valid datasets.

Fixes SENTRY-5PEF

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LjN9gb92hUE6jTqQ9dkjAm
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Jul 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Backend Test Failures

Failures on a32dc8b in this run:

tests/sentry/snuba/test_validators.py::SnubaQueryValidatorTest::test_invalid_dataset_sessions_returns_validation_errorlog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/snuba/test_validators.py:84: in test_invalid_dataset_sessions_returns_validation_error
    assert any("Invalid dataset for alerts" in str(e) for e in non_field_errors)
E   assert False
E    +  where False = any(<generator object SnubaQueryValidatorTest.test_invalid_dataset_sessions_returns_validation_error.<locals>.<genexpr> at 0x7f45f52843c0>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants