refactor: replace generic Exception handling with specific exceptions in various files
This commit is contained in:
@@ -845,7 +845,7 @@ async def broadcast_gateway_lead_message(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
sent += 1
|
sent += 1
|
||||||
except Exception as exc:
|
except (HTTPException, OpenClawGatewayError, ValueError) as exc:
|
||||||
results.append(
|
results.append(
|
||||||
GatewayLeadBroadcastBoardResult(
|
GatewayLeadBroadcastBoardResult(
|
||||||
board_id=board.id,
|
board_id=board.id,
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from app.core.time import utcnow
|
|||||||
from app.db import crud
|
from app.db import crud
|
||||||
from app.db.pagination import paginate
|
from app.db.pagination import paginate
|
||||||
from app.db.session import get_session
|
from app.db.session import get_session
|
||||||
|
from app.integrations.openclaw_gateway import OpenClawGatewayError
|
||||||
from app.models.agents import Agent
|
from app.models.agents import Agent
|
||||||
from app.models.board_group_memory import BoardGroupMemory
|
from app.models.board_group_memory import BoardGroupMemory
|
||||||
from app.models.board_groups import BoardGroup
|
from app.models.board_groups import BoardGroup
|
||||||
@@ -240,7 +241,7 @@ async def apply_board_group_heartbeat(
|
|||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
await sync_gateway_agent_heartbeats(gateway, gateway_agents)
|
await sync_gateway_agent_heartbeats(gateway, gateway_agents)
|
||||||
except Exception:
|
except OpenClawGatewayError:
|
||||||
failed_agent_ids.extend([agent.id for agent in gateway_agents])
|
failed_agent_ids.extend([agent.id for agent in gateway_agents])
|
||||||
|
|
||||||
return BoardGroupHeartbeatApplyResult(
|
return BoardGroupHeartbeatApplyResult(
|
||||||
|
|||||||
@@ -71,14 +71,13 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
|||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
finally:
|
finally:
|
||||||
|
in_txn = False
|
||||||
try:
|
try:
|
||||||
in_txn = bool(session.in_transaction())
|
in_txn = bool(session.in_transaction())
|
||||||
except SQLAlchemyError:
|
except SQLAlchemyError:
|
||||||
logger.exception("Failed to inspect session transaction state.")
|
logger.exception("Failed to inspect session transaction state.")
|
||||||
return
|
if in_txn:
|
||||||
if not in_txn:
|
try:
|
||||||
return
|
await session.rollback()
|
||||||
try:
|
except SQLAlchemyError:
|
||||||
await session.rollback()
|
logger.exception("Failed to rollback session after request error.")
|
||||||
except SQLAlchemyError:
|
|
||||||
logger.exception("Failed to rollback session after request error.")
|
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class _GatewayBackoff:
|
|||||||
value = await fn()
|
value = await fn()
|
||||||
self.reset()
|
self.reset()
|
||||||
return value
|
return value
|
||||||
except Exception as exc:
|
except OpenClawGatewayError as exc:
|
||||||
if not _is_transient_gateway_error(exc):
|
if not _is_transient_gateway_error(exc):
|
||||||
raise
|
raise
|
||||||
now = asyncio.get_running_loop().time()
|
now = asyncio.get_running_loop().time()
|
||||||
@@ -430,13 +430,20 @@ async def sync_gateway_templates(
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
agent_item: Agent = agent
|
||||||
|
board_item: Board = board
|
||||||
|
auth_token_value: str = auth_token
|
||||||
|
|
||||||
async def _do_provision() -> None:
|
async def _do_provision(
|
||||||
|
agent_item: Agent = agent_item,
|
||||||
|
board_item: Board = board_item,
|
||||||
|
auth_token_value: str = auth_token_value,
|
||||||
|
) -> None:
|
||||||
await provision_agent(
|
await provision_agent(
|
||||||
agent,
|
agent_item,
|
||||||
board,
|
board_item,
|
||||||
gateway,
|
gateway,
|
||||||
auth_token,
|
auth_token_value,
|
||||||
user,
|
user,
|
||||||
action="update",
|
action="update",
|
||||||
force_bootstrap=force_bootstrap,
|
force_bootstrap=force_bootstrap,
|
||||||
@@ -456,7 +463,7 @@ async def sync_gateway_templates(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
except Exception as exc: # pragma: no cover - gateway/network dependent
|
except (OSError, RuntimeError, ValueError) as exc: # pragma: no cover
|
||||||
result.agents_skipped += 1
|
result.agents_skipped += 1
|
||||||
result.errors.append(
|
result.errors.append(
|
||||||
GatewayTemplatesSyncError(
|
GatewayTemplatesSyncError(
|
||||||
@@ -584,7 +591,7 @@ async def sync_gateway_templates(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
except Exception as exc: # pragma: no cover - gateway/network dependent
|
except (OSError, RuntimeError, ValueError) as exc: # pragma: no cover
|
||||||
result.errors.append(
|
result.errors.append(
|
||||||
GatewayTemplatesSyncError(
|
GatewayTemplatesSyncError(
|
||||||
agent_id=main_agent.id,
|
agent_id=main_agent.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user