fix(api): export endpoint returns 409 when QA gate blocks (FU-6 UX — avoid false success toast)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 18:03:21 +00:00
parent 084b31cd9b
commit a61495f5ef

View File

@@ -3170,10 +3170,25 @@ async def api_export_docx(case_number: str, background_tasks: BackgroundTasks):
(markdown body + download link) to the linked issue.
"""
result = await drafting_tools.export_docx(case_number)
try:
data = json.loads(result)
except json.JSONDecodeError:
raise HTTPException(500, result)
if isinstance(result, dict):
data = result
else:
try:
data = json.loads(result)
except (json.JSONDecodeError, TypeError):
# export_docx can also return a plain (non-JSON) string, e.g.
# "תיק ... לא נמצא." — surface it as a 500 with the raw text.
raise HTTPException(500, str(result))
# FU-6: a QA gate (or another error) can block the export. export_docx
# signals this with status == "error". Returning the existing 200 here
# would let the UI show a false "exported successfully" toast, so we map
# a block to 409 Conflict carrying the Hebrew message + failed_gates.
if isinstance(data, dict) and data.get("status") == "error":
detail = {"message": data.get("message", "ייצוא נחסם.")}
if data.get("failed_gates"):
detail["failed_gates"] = data["failed_gates"]
raise HTTPException(409, detail)
# Notify the Paperclip plugin to attach the final-decision document.
docx_filename = (