Plain text SQL Interrupted errors in JSON responses

The SQL time limit error embedded an HTML fragment (paragraph, textarea
and script tags) as the error string in JSON responses. DatasetteError
now accepts a plain_message which the exception handler prefers for
JSON error bodies; the HTML error page keeps the rich message with the
SQL textarea.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GrHZSypDfMnym1tM5XJAFZ
This commit is contained in:
Claude 2026-07-06 23:34:17 +00:00
commit 0d962deb05
No known key found for this signature in database
7 changed files with 43 additions and 12 deletions

View file

@ -329,14 +329,8 @@ def test_sql_time_limit(app_client_shorter_time_limit):
)
assert 400 == response.status
expected_message = (
"<p>SQL query took too long. The time limit is controlled by the\n"
'<a href="https://docs.datasette.io/en/stable/settings.html#sql-time-limit-ms">sql_time_limit_ms</a>\n'
"configuration option.</p>\n"
'<textarea style="width: 90%">select sleep(0.5)</textarea>\n'
"<script>\n"
'let ta = document.querySelector("textarea");\n'
'ta.style.height = ta.scrollHeight + "px";\n'
"</script>"
"SQL query took too long. The time limit is"
" controlled by the sql_time_limit_ms setting."
)
assert response.json == {
"ok": False,
@ -356,7 +350,7 @@ async def test_custom_sql_time_limit(ds_client):
"/fixtures/-/query.json?sql=select+sleep(0.01)&_timelimit=5",
)
assert response.status_code == 400
assert response.json()["error"].startswith("<p>SQL query took too long.")
assert response.json()["error"].startswith("SQL query took too long.")
@pytest.mark.asyncio

View file

@ -717,3 +717,25 @@ async def test_alter_and_set_column_type_ignore_content_type(ds_error_shape):
actor={"id": "root"},
)
assert sct.status_code == 200, sct.text
# SQL Interrupted errors carry plain text in JSON, not an HTML fragment
@pytest.mark.asyncio
async def test_sql_interrupted_json_error_is_plain_text(ds_client):
response = await ds_client.get(
"/fixtures/-/query.json?sql=select+sleep(0.01)&_timelimit=5"
)
data = assert_canonical_error(response, 400)
assert "<" not in data["error"]
assert data["error"].startswith("SQL query took too long.")
@pytest.mark.asyncio
async def test_sql_interrupted_html_page_keeps_rich_error(ds_client):
response = await ds_client.get(
"/fixtures/-/query?sql=select+sleep(0.01)&_timelimit=5"
)
assert response.status_code == 400
assert "<textarea" in response.text