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:
@@ -23,7 +23,8 @@ def _get_engine_and_session():
|
||||
if not os.path.isabs(db_path):
|
||||
db_path = os.path.abspath(db_path)
|
||||
if not os.path.exists(db_path):
|
||||
raise FileNotFoundError(f"数字分身数据库不存在: {db_path}")
|
||||
# 数据库不存在时返回 None,由调用方处理
|
||||
return None, None
|
||||
_engine = create_engine(
|
||||
f"sqlite:///{db_path}",
|
||||
connect_args={"check_same_thread": False},
|
||||
@@ -32,11 +33,17 @@ def _get_engine_and_session():
|
||||
return _engine, _SessionLocal()
|
||||
|
||||
|
||||
def get_session() -> Session:
|
||||
def get_session() -> Optional[Session]:
|
||||
_, session = _get_engine_and_session()
|
||||
return session
|
||||
|
||||
|
||||
def is_available() -> bool:
|
||||
"""检查数字分身数据库是否可用"""
|
||||
engine, _ = _get_engine_and_session()
|
||||
return engine is not None
|
||||
|
||||
|
||||
class AvatarService:
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user