From 7e6aebeb76666477e153a2ca33f457c4f68af94d Mon Sep 17 00:00:00 2001 From: umsangdon Date: Tue, 6 Oct 2026 15:55:01 +0900 Subject: [PATCH] fix: distinguish Gemini timeout --- B01_Dashboard/B01_Dashboard_Knowledge_Chat.py | 6 +++++ resources/tester/test_b01_knowledge_chat.py | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/B01_Dashboard/B01_Dashboard_Knowledge_Chat.py b/B01_Dashboard/B01_Dashboard_Knowledge_Chat.py index 653e64637..6ada34528 100644 --- a/B01_Dashboard/B01_Dashboard_Knowledge_Chat.py +++ b/B01_Dashboard/B01_Dashboard_Knowledge_Chat.py @@ -179,6 +179,12 @@ async def ask_knowledge_chat(payload: ChatRequest, _session=Depends(verify_sessi 429: "Gemini API 사용 한도에 도달했습니다. 잠시 후 다시 시도해 주세요.", }.get(exc.response.status_code, "Gemini 요청에 실패했습니다. 잠시 후 다시 시도해 주세요.") raise HTTPException(status_code=502, detail=detail) from exc + except httpx.TimeoutException as exc: + raise HTTPException( + status_code=504, + detail="Gemini 3.8 Flash 응답이 지연되어 시간 초과됐습니다. " + "잠시 후 다시 시도해 주세요.", + ) from exc except (httpx.RequestError, ValueError) as exc: raise HTTPException( status_code=502, diff --git a/resources/tester/test_b01_knowledge_chat.py b/resources/tester/test_b01_knowledge_chat.py index 81ef1c8be..fb647702e 100644 --- a/resources/tester/test_b01_knowledge_chat.py +++ b/resources/tester/test_b01_knowledge_chat.py @@ -1,5 +1,6 @@ import asyncio +import httpx import pytest from fastapi import HTTPException @@ -65,6 +66,30 @@ def test_busy_gemini_is_retried_and_reported(monkeypatch): assert "요청이 몰려" in error.value.detail +def test_gemini_timeout_is_reported(monkeypatch): + monkeypatch.setenv("GEMINI_API_KEY", "test-key") + monkeypatch.setattr(chat, "search", lambda _question: []) + + class Client: + def __init__(self, timeout): + pass + + async def __aenter__(self): + return self + + async def __aexit__(self, *_args): + pass + + async def post(self, *_args, **_kwargs): + raise httpx.ReadTimeout("timed out") + + monkeypatch.setattr(chat.httpx, "AsyncClient", Client) + with pytest.raises(HTTPException) as error: + asyncio.run(chat.ask_knowledge_chat(chat.ChatRequest(question="일위대가가 뭐야"), {})) + assert error.value.status_code == 504 + assert "시간 초과" in error.value.detail + + def test_chat_searches_all_knowledge_markdown_except_excluded_folders(monkeypatch, tmp_path): public = tmp_path / "original" / "법률" guide = tmp_path / "technical_info"