Skip to content

Conversation

@Ulthran
Copy link
Contributor

@Ulthran Ulthran commented Jan 23, 2026

Motivation

  • Prevent 500 errors from DataTables endpoints when underlying tables or queries are missing or invalid by returning a consistent empty DataTables response.
  • Ensure both raw SQL and SQLAlchemy Select code paths behave consistently when the database cannot execute the query.

Description

  • Add imports for OperationalError and ProgrammingError from sqlalchemy.exc and create an empty_response helper to return an empty DataTables-shaped response.
  • Wrap raw SQL path column discovery and counting/pagination logic in try/except blocks that return empty_response on OperationalError/ProgrammingError.
  • Wrap the SQLAlchemy Select path (counts, filtering, ordering, pagination) in a try/except block that returns empty_response on database errors.
  • Move retrieval of request.values to the function start so it can be reused and ensure response draw/columns remain consistent.

Testing

  • Ran code formatting with black . which completed successfully.
  • Ran pytest tests/ and all tests passed: 9 passed.

Codex Task

Copilot AI review requested due to automatic review settings January 23, 2026 16:29
@Ulthran Ulthran added the codex label Jan 23, 2026
@Ulthran Ulthran merged commit 5197921 into main Jan 23, 2026
8 of 9 checks passed
@Ulthran Ulthran deleted the codex/fix-failing-tests-in-test_api.py branch January 23, 2026 16:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds error handling to the datatables_response function to prevent 500 errors when database queries fail due to missing tables or invalid SQL. Instead of crashing, the function now returns an empty DataTables-formatted response.

Changes:

  • Added database exception imports (OperationalError, ProgrammingError) and an empty_response helper function
  • Wrapped raw SQL path operations (column discovery and query execution) in try/except blocks
  • Wrapped SQLAlchemy Select path operations in a try/except block
  • Moved request.values retrieval to function start for consistency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 140 to 141
columns = [c.key for c in query.selected_columns]
base_query = query.with_only_columns(query.selected_columns)
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lines that access query.selected_columns (lines 140-141) are executed before the try/except block begins at line 142. If the query references a non-existent table or has other structural issues, accessing selected_columns or calling with_only_columns() could raise an OperationalError or ProgrammingError before error handling is active. Move lines 140-141 inside the try block to ensure all operations that could fail due to database issues are protected.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants