fix(stats): _load_stats_data() JSON 解析失败时备份损坏文件

- JSON 解析失败时将损坏文件重命名为 .corrupted.{timestamp}.bak
- 保留损坏文件以便后续分析和恢复
- 分离 JSONDecodeError 和 IOError 的处理逻辑
This commit is contained in:
2026-03-09 14:49:12 +08:00
parent d26ac7a88c
commit 9b15766ab3
@@ -34,7 +34,15 @@ def _load_stats_data() -> Dict[str, Any]:
try:
with open(STATS_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
except json.JSONDecodeError as e:
# 备份损坏的文件
backup_path = STATS_FILE.with_suffix(f'.corrupted.{datetime.now().strftime("%Y%m%d_%H%M%S")}.bak')
try:
STATS_FILE.rename(backup_path)
except OSError:
pass # 备份失败时忽略
return {"events": [], "metadata": {"created_at": datetime.now().isoformat()}}
except IOError:
return {"events": [], "metadata": {"created_at": datetime.now().isoformat()}}