Skip to content

Commit e22bf7a

Browse files
authored
fix: preserve HTTPException status in update_docling_preset (#1586) (#2004)
1 parent ecdaea8 commit e22bf7a

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/api/settings/endpoints.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,12 @@ async def update_docling_preset(
16011601
settings=settings_toggles,
16021602
preset_config=preset_config,
16031603
)
1604-
1604+
except HTTPException:
1605+
# Preserve intended HTTP status codes (e.g. 400 for an invalid preset)
1606+
raise
16051607
except Exception as e:
16061608
logger.error("Failed to update docling settings", error=str(e))
1607-
raise HTTPException(
1608-
status_code=500, detail=f"Failed to update docling settings: {str(e)}"
1609-
) from e
1609+
raise HTTPException(status_code=500, detail="Failed to update docling settings") from e
16101610

16111611

16121612
async def refresh_openrag_docs(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Unit tests for api.settings.endpoints
3+
Validates error handling in update_docling_preset endpoint.
4+
"""
5+
6+
from unittest.mock import AsyncMock, MagicMock
7+
8+
import pytest
9+
from fastapi import HTTPException
10+
11+
from api.settings import DoclingPresetBody, update_docling_preset
12+
from session_manager import User
13+
14+
15+
@pytest.mark.asyncio
16+
async def test_update_docling_preset_invalid_preset_returns_400():
17+
"""Test that an invalid preset value returns 400 status code.
18+
19+
This test ensures that the HTTPException with status_code=400 raised
20+
for invalid presets is not masked by the broad Exception handler.
21+
Regression test for issue #1586.
22+
"""
23+
# Create a body with an invalid preset
24+
body = DoclingPresetBody(preset="nonexistent_preset")
25+
26+
# Mock dependencies
27+
session_manager = AsyncMock()
28+
user = MagicMock(spec=User)
29+
30+
# Call the endpoint and expect HTTPException with 400
31+
with pytest.raises(HTTPException) as exc_info:
32+
await update_docling_preset(body=body, session_manager=session_manager, user=user)
33+
34+
# Assert it's a 400 error (not 500)
35+
assert exc_info.value.status_code == 400
36+
assert "Invalid preset" in exc_info.value.detail
37+
assert "nonexistent_preset" in exc_info.value.detail

0 commit comments

Comments
 (0)