Return 400 instead of 500 when query name/description exceed column limits

EK
3d ago
+45-0

What type of PR is this?

  • Bug Fix

Description

Fixes #7478.

Creating (or updating) a query with a name longer than 255 characters returned a 500: the handler passed the value straight through to the database, where the insert failed with psycopg2.errors.StringDataRightTruncation (value too long for type character varying(255)). The same applies to description (varchar(4096)).

This adds a small require_valid_field_lengths() check in redash/handlers/queries.py, used by both QueryListResource.post (create) and QueryResource.post (update). It reads the maximum lengths from the model's column definitions (no duplicated magic numbers) and aborts with 400 and a message like Query name must be at most 255 characters long.

How is this tested?

  • Unit tests (pytest, jest)

New tests in tests/handlers/test_queries.py:

  • TestQueryListResourcePost::test_rejects_too_long_name
  • TestQueryListResourcePost::test_rejects_too_long_description
  • TestQueryResourcePost::test_update_query_rejects_too_long_name

Before the fix these fail with sqlalchemy.exc.DataError (the 500 from the issue); with the fix pytest tests/handlers/test_queries.py passes (37 passed). ruff check and black --check are clean.

Related Tickets & Documents

#7478

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

N/A (backend only)