fix: gracefully handle missing avatar SQLite database
- Return empty list instead of crashing when avatar.db doesn't exist - Add is_available() check before each endpoint - 503 status when trying to access unavailable database
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
from fastapi import APIRouter, Query, HTTPException
|
||||
|
||||
from app.schemas import ApiResponse
|
||||
from app.services.avatar_service import avatar_service, get_session
|
||||
from app.services.avatar_service import avatar_service, get_session, is_available
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -15,6 +15,8 @@ def list_avatars(
|
||||
status: str = Query(default=None),
|
||||
):
|
||||
"""分页查询所有数字分身"""
|
||||
if not is_available():
|
||||
return ApiResponse(data={"total": 0, "page": page, "page_size": page_size, "items": []})
|
||||
db = get_session()
|
||||
try:
|
||||
total, items = avatar_service.list_avatars(
|
||||
@@ -35,6 +37,8 @@ def list_avatars(
|
||||
@router.get("/{avatar_id}")
|
||||
def get_avatar(avatar_id: str):
|
||||
"""获取单个数字分身详情"""
|
||||
if not is_available():
|
||||
raise HTTPException(status_code=503, detail="数字分身数据库尚未初始化")
|
||||
db = get_session()
|
||||
try:
|
||||
data = avatar_service.get_avatar(db, avatar_id)
|
||||
@@ -54,6 +58,8 @@ def update_avatar_status(avatar_id: str, body: dict):
|
||||
status_code=400,
|
||||
detail="status 必须是 active、inactive 或 training",
|
||||
)
|
||||
if not is_available():
|
||||
raise HTTPException(status_code=503, detail="数字分身数据库尚未初始化")
|
||||
db = get_session()
|
||||
try:
|
||||
data = avatar_service.update_status(db, avatar_id, status)
|
||||
|
||||
Reference in New Issue
Block a user