Skip to content

Commit 65bd167

Browse files
BigOrangeQWQhe0119Copilot
authored
fix: 为解码 JSON 相关代码增加错误捕获 (#444)
Co-authored-by: uy/sun <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 3d132c9 commit 65bd167

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/providers/docker_test/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ async def run(self, version: str) -> DockerTestResult:
7777
detach=False,
7878
remove=True,
7979
).decode()
80-
data = json.loads(output)
80+
81+
try:
82+
data = json.loads(output)
83+
except json.JSONDecodeError:
84+
data = {
85+
"run": True,
86+
"load": False,
87+
"output": f"插件测试结果解析失败,输出内容非 JSON 格式。\n输出内容:{output}",
88+
}
8189
except Exception as e:
8290
data = {
8391
"run": False,

src/providers/docker_test/plugin_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,15 @@ async def run(self):
171171
# 补上获取到 Python 版本
172172
self._test_env.insert(0, f"python=={self._test_python_version}")
173173
# 读取插件元数据
174-
metadata = None
175174
metadata_path = self._test_dir / "metadata.json"
175+
metadata = None
176176
if metadata_path.exists():
177-
with open(self._test_dir / "metadata.json", encoding="utf-8") as f:
178-
metadata = json.load(f)
177+
try:
178+
metadata = json.loads(metadata_path.read_text(encoding="utf-8"))
179+
except json.JSONDecodeError:
180+
self._log_output("插件元数据读取失败,metadata.json 格式不正确。")
181+
except Exception as e:
182+
self._log_output(f"插件元数据读取失败,错误信息:{e}")
179183

180184
result = {
181185
"metadata": metadata,

0 commit comments

Comments
 (0)