refactor: enhance error handling with structured logging across multiple modules

- Updated various modules including cli.py, run_agent.py, gateway, and tools to replace silent exception handling with structured logging.
- Improved error messages to provide more context, aiding in debugging and monitoring.
- Ensured consistent logging practices throughout the codebase, enhancing traceability and maintainability.
This commit is contained in:
teknium1 2026-02-21 03:32:11 -08:00
parent cbff1b818c
commit 748fd3db88
14 changed files with 134 additions and 110 deletions

View file

@ -8,9 +8,12 @@ Uses discord.py library for:
"""
import asyncio
import logging
import os
from typing import Dict, List, Optional, Any
logger = logging.getLogger(__name__)
try:
import discord
from discord import Message as DiscordMessage, Intents
@ -177,8 +180,8 @@ class DiscordAdapter(BasePlatformAdapter):
try:
ref_msg = await channel.fetch_message(int(reply_to))
reference = ref_msg
except Exception:
pass # Ignore if we can't find the referenced message
except Exception as e:
logger.debug("Could not fetch reply-to message: %s", e)
for i, chunk in enumerate(chunks):
msg = await channel.send(
@ -363,8 +366,8 @@ class DiscordAdapter(BasePlatformAdapter):
# Send a followup to close the interaction if needed
try:
await interaction.followup.send("Processing complete~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="new", description="Start a new conversation")
async def slash_new(interaction: discord.Interaction):
@ -373,8 +376,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("New conversation started~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="reset", description="Reset your Hermes session")
async def slash_reset(interaction: discord.Interaction):
@ -383,8 +386,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Session reset~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="model", description="Show or change the model")
@discord.app_commands.describe(name="Model name (e.g. anthropic/claude-sonnet-4). Leave empty to see current.")
@ -394,8 +397,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Done~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="personality", description="Set a personality")
@discord.app_commands.describe(name="Personality name. Leave empty to list available.")
@ -405,8 +408,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Done~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="retry", description="Retry your last message")
async def slash_retry(interaction: discord.Interaction):
@ -415,8 +418,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Retrying~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="undo", description="Remove the last exchange")
async def slash_undo(interaction: discord.Interaction):
@ -425,8 +428,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Done~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="status", description="Show Hermes session status")
async def slash_status(interaction: discord.Interaction):
@ -435,8 +438,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Status sent~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
@tree.command(name="stop", description="Stop the running Hermes agent")
async def slash_stop(interaction: discord.Interaction):
@ -445,8 +448,8 @@ class DiscordAdapter(BasePlatformAdapter):
await self.handle_message(event)
try:
await interaction.followup.send("Stop requested~", ephemeral=True)
except Exception:
pass
except Exception as e:
logger.debug("Discord followup failed: %s", e)
def _build_slash_event(self, interaction: discord.Interaction, text: str) -> MessageEvent:
"""Build a MessageEvent from a Discord slash command interaction."""

View file

@ -17,10 +17,13 @@ with different backends via a bridge pattern.
import asyncio
import json
import logging
import subprocess
from pathlib import Path
from typing import Dict, List, Optional, Any
logger = logging.getLogger(__name__)
import sys
sys.path.insert(0, str(__file__).rsplit("/", 3)[0])
@ -246,8 +249,8 @@ class WhatsAppAdapter(BasePlatformAdapter):
"type": "group" if data.get("isGroup") else "dm",
"participants": data.get("participants", []),
}
except Exception:
pass
except Exception as e:
logger.debug("Could not get WhatsApp chat info for %s: %s", chat_id, e)
return {"name": chat_id, "type": "dm"}

View file

@ -479,8 +479,8 @@ class GatewayRunner:
self._pending_approvals[session_key] = _last_pending_approval.copy()
# Clear the global so it doesn't leak to other sessions
_last_pending_approval.clear()
except Exception:
pass
except Exception as e:
logger.debug("Failed to check pending approvals: %s", e)
# Save the full conversation to the transcript, including tool calls.
# This preserves the complete agent loop (tool_calls, tool results,
@ -973,8 +973,8 @@ class GatewayRunner:
with open(config_path, 'r') as f:
user_config = yaml.safe_load(f) or {}
platform_toolsets_config = user_config.get("platform_toolsets", {})
except Exception:
pass
except Exception as e:
logger.debug("Could not load platform_toolsets config: %s", e)
# Map platform enum to config key
platform_config_key = {

View file

@ -8,6 +8,7 @@ Handles:
- Dynamic system prompt injection (agent knows its context)
"""
import logging
import os
import json
import uuid
@ -16,6 +17,8 @@ from datetime import datetime, timedelta
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Any
logger = logging.getLogger(__name__)
from .config import (
Platform,
GatewayConfig,
@ -388,8 +391,8 @@ class SessionStore:
if self._db:
try:
self._db.end_session(entry.session_id, "session_reset")
except Exception:
pass
except Exception as e:
logger.debug("Session DB operation failed: %s", e)
# Create new session
session_id = f"{now.strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}"
@ -443,8 +446,8 @@ class SessionStore:
self._db.update_token_counts(
entry.session_id, input_tokens, output_tokens
)
except Exception:
pass
except Exception as e:
logger.debug("Session DB operation failed: %s", e)
def reset_session(self, session_key: str) -> Optional[SessionEntry]:
"""Force reset a session, creating a new session ID."""
@ -459,8 +462,8 @@ class SessionStore:
if self._db:
try:
self._db.end_session(old_entry.session_id, "session_reset")
except Exception:
pass
except Exception as e:
logger.debug("Session DB operation failed: %s", e)
now = datetime.now()
session_id = f"{now.strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}"
@ -487,8 +490,8 @@ class SessionStore:
source=old_entry.platform.value if old_entry.platform else "unknown",
user_id=old_entry.origin.user_id if old_entry.origin else None,
)
except Exception:
pass
except Exception as e:
logger.debug("Session DB operation failed: %s", e)
return new_entry
@ -523,8 +526,8 @@ class SessionStore:
tool_calls=message.get("tool_calls"),
tool_call_id=message.get("tool_call_id"),
)
except Exception:
pass
except Exception as e:
logger.debug("Session DB operation failed: %s", e)
# Also write legacy JSONL (keeps existing tooling working during transition)
transcript_path = self.get_transcript_path(session_id)
@ -539,8 +542,8 @@ class SessionStore:
messages = self._db.get_messages_as_conversation(session_id)
if messages:
return messages
except Exception:
pass
except Exception as e:
logger.debug("Could not load messages from DB: %s", e)
# Fall back to legacy JSONL
transcript_path = self.get_transcript_path(session_id)