fix(qzone_utils): 增强从NapCat获取Cookie的异常处理和日志记录,优化请求超时时间,确保在连接失败时提供详细错误信息

This commit is contained in:
minecraft1024a
2025-08-12 15:30:19 +08:00
committed by Windpicker-owo
parent f02e96d288
commit 6ac696a7ce

View File

@@ -65,16 +65,32 @@ class CookieManager:
async def fetch_cookies(domain: str, port: str, host: str) -> Dict[str, Any]: async def fetch_cookies(domain: str, port: str, host: str) -> Dict[str, Any]:
"""从NapCat获取Cookie""" """从NapCat获取Cookie"""
url = f"http://{host}:{port}/get_cookies?domain={domain}" url = f"http://{host}:{port}/get_cookies?domain={domain}"
logger.info(f"正在从NapCat获取CookieURL: {url}")
try: try:
async with httpx.AsyncClient(timeout=10.0, trust_env=False) as client: async with httpx.AsyncClient(timeout=20.0, trust_env=False) as client:
logger.debug(f"发送GET请求到: {url}")
resp = await client.get(url) resp = await client.get(url)
logger.debug(f"响应状态码: {resp.status_code}")
resp.raise_for_status() resp.raise_for_status()
data = resp.json() data = resp.json()
logger.info(f"响应数据: {data}")
if data.get("status") != "ok" or "cookies" not in data.get("data", {}): if data.get("status") != "ok" or "cookies" not in data.get("data", {}):
raise RuntimeError(f"获取Cookie失败: {data}") raise RuntimeError(f"获取Cookie失败: {data}")
logger.info("成功从NapCat获取Cookie")
return data["data"] return data["data"]
except httpx.ConnectError as e:
logger.error(f"无法连接到NapCat服务器 {host}:{port} - 请检查NapCat是否运行: {str(e)}")
raise
except httpx.TimeoutException as e:
logger.error(f"连接NapCat超时: {str(e)}")
raise
except httpx.HTTPStatusError as e:
logger.error(f"NapCat返回错误状态码 {e.response.status_code}: {e.response.text}")
raise
except Exception as e: except Exception as e:
logger.error(f"从NapCat获取Cookie失败: {str(e)}") logger.error(f"从NapCat获取Cookie失败: {str(e)}")
raise raise