refactor: replace try-except blocks with contextlib.suppress for cleaner error handling across multiple files
This commit is contained in:
@@ -4,7 +4,7 @@ import asyncio
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
from contextlib import AsyncExitStack
|
||||
from contextlib import AsyncExitStack, suppress
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -609,10 +609,8 @@ async def connect_mcp_servers(
|
||||
"only JSON-RPC to stdout and sends logs/debug output to stderr instead."
|
||||
)
|
||||
logger.error("MCP server '{}': failed to connect: {}{}", name, e, hint)
|
||||
try:
|
||||
with suppress(Exception):
|
||||
await server_stack.aclose()
|
||||
except Exception:
|
||||
pass
|
||||
return name, None
|
||||
|
||||
server_stacks: dict[str, AsyncExitStack] = {}
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import fnmatch
|
||||
import os
|
||||
import re
|
||||
from contextlib import suppress
|
||||
from pathlib import Path, PurePosixPath
|
||||
from typing import Any, Iterable, TypeVar
|
||||
|
||||
@@ -92,10 +93,8 @@ class _SearchTool(_FsTool):
|
||||
|
||||
def _display_path(self, target: Path, root: Path) -> str:
|
||||
if self._workspace:
|
||||
try:
|
||||
with suppress(ValueError):
|
||||
return target.relative_to(self._workspace).as_posix()
|
||||
except ValueError:
|
||||
pass
|
||||
return target.relative_to(root).as_posix()
|
||||
|
||||
def _iter_files(self, root: Path) -> Iterable[Path]:
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -212,9 +213,8 @@ class ExecTool(Tool):
|
||||
"""Kill a subprocess and reap it to prevent zombies."""
|
||||
process.kill()
|
||||
try:
|
||||
await asyncio.wait_for(process.wait(), timeout=5.0)
|
||||
except asyncio.TimeoutError:
|
||||
pass
|
||||
with suppress(asyncio.TimeoutError):
|
||||
await asyncio.wait_for(process.wait(), timeout=5.0)
|
||||
finally:
|
||||
if not _IS_WINDOWS:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user