fix(cookbook): resolve NameError in hf-gguf-files exception handler (#6341) (#6369)

In routes/cookbook_routes.py, the exception handler for GET /api/cookbook/hf-gguf-files referenced repo instead of the parameter repo_id, causing an unhandled NameError and 500 status when upstream requests to Hugging Face failed.

Fixes #6341.
This commit is contained in:
Aashish Mahato
2026-10-31 12:59:17 +00:00
committed by GitHub
parent bebf53be3c
commit 9a1fdb192e
2 changed files with 63 additions and 1 deletions
+1 -1
View File
@@ -3930,7 +3930,7 @@ def setup_cookbook_routes() -> APIRouter:
return {"ok": False, "files": [], "error": f"HF API HTTP {resp.status_code}"}
data = resp.json()
except Exception:
logger.exception("HF GGUF file scan failed for %s", repo)
logger.exception("HF GGUF file scan failed for %s", repo_id)
return {"ok": False, "files": [], "error": "HF API request failed"}
files = [
str(s.get("rfilename") or "")
+62
View File
@@ -0,0 +1,62 @@
import pytest
import httpx
from unittest.mock import patch, AsyncMock
from fastapi import FastAPI
from fastapi.testclient import TestClient
from routes.cookbook_routes import setup_cookbook_routes
from src.auth_helpers import require_user
app = FastAPI()
router = setup_cookbook_routes()
app.include_router(router)
app.dependency_overrides[require_user] = lambda: "test_user"
client = TestClient(app)
def test_hf_gguf_files_success():
mock_resp = httpx.Response(
200,
json={
"siblings": [
{"rfilename": "model-q4_k_m.gguf"},
{"rfilename": "model-q8_0.gguf"},
{"rfilename": "README.md"},
]
},
request=httpx.Request("GET", "https://huggingface.co/api/models/TheBloke/Llama-2-7B-GGUF"),
)
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
assert response.status_code == 200
data = response.json()
assert data["ok"] is True
assert data["repo_id"] == "TheBloke/Llama-2-7B-GGUF"
assert data["files"] == ["model-q4_k_m.gguf", "model-q8_0.gguf"]
def test_hf_gguf_files_http_error():
mock_resp = httpx.Response(
404,
json={"error": "Model not found"},
request=httpx.Request("GET", "https://huggingface.co/api/models/invalid/repo"),
)
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=invalid/repo")
assert response.status_code == 200
data = response.json()
assert data["ok"] is False
assert "HF API HTTP 404" in data["error"]
def test_hf_gguf_files_exception_handling():
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, side_effect=httpx.ConnectError("Network unreachable")):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
# Should gracefully return JSON error payload, not raise NameError / 500
assert response.status_code == 200
data = response.json()
assert data["ok"] is False
assert data["error"] == "HF API request failed"