feat(deploy-control): 添加命令执行异常处理

当subprocess.run执行失败时捕获OSError异常,并抛出带有详细错误信息的ApiError,
提供更好的错误提示和调试支持。
```
This commit is contained in:
2026-06-15 18:09:25 +08:00
parent beddf12bc0
commit 06971dc673
2 changed files with 41 additions and 8 deletions

View File

@ -100,14 +100,18 @@ def run_command(args: list[str], *, cwd: Path | None = None, extra_env: dict[str
env = os.environ.copy()
if extra_env:
env.update(extra_env)
completed = subprocess.run(
args,
cwd=str(cwd) if cwd else None,
env=env,
text=True,
capture_output=True,
check=False,
)
try:
completed = subprocess.run(
args,
cwd=str(cwd) if cwd else None,
env=env,
text=True,
capture_output=True,
check=False,
)
except OSError as exc:
command = args[0] if args else "<empty command>"
raise ApiError(HTTPStatus.BAD_GATEWAY, f"failed to execute {command}: {exc}") from exc
if completed.returncode != 0:
detail = completed.stderr.strip() or completed.stdout.strip() or "command failed"
raise ApiError(HTTPStatus.BAD_GATEWAY, detail)