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

@ -16,6 +16,7 @@ Architecture:
from __future__ import annotations
import json
import logging
import os
import stat
import time
@ -32,6 +33,8 @@ import yaml
from hermes_cli.config import get_hermes_home, get_config_path
from hermes_constants import OPENROUTER_BASE_URL
logger = logging.getLogger(__name__)
try:
import fcntl
except Exception:
@ -314,8 +317,8 @@ def resolve_provider(
state = _load_provider_state(auth_store, active)
if state and (state.get("access_token") or state.get("refresh_token")):
return active
except Exception:
pass
except Exception as e:
logger.debug("Could not detect active auth provider: %s", e)
if os.getenv("OPENAI_API_KEY") or os.getenv("OPENROUTER_API_KEY"):
return "openrouter"
@ -578,8 +581,8 @@ def fetch_nous_models(
try:
err = response.json()
description = str(err.get("error_description") or err.get("error") or description)
except Exception:
pass
except Exception as e:
logger.debug("Could not parse error response JSON: %s", e)
raise AuthError(description, provider="nous", code="models_fetch_failed")
payload = response.json()

View file

@ -39,9 +39,13 @@ env_path = PROJECT_ROOT / '.env'
if env_path.exists():
load_dotenv(dotenv_path=env_path)
import logging
from hermes_cli import __version__
from hermes_constants import OPENROUTER_BASE_URL
logger = logging.getLogger(__name__)
def cmd_chat(args):
"""Run interactive chat CLI."""
@ -512,8 +516,8 @@ def cmd_update(args):
print(f" + {len(result['copied'])} new skill(s): {', '.join(result['copied'])}")
else:
print(" ✓ Skills are up to date")
except Exception:
pass
except Exception as e:
logger.debug("Skills sync during update failed: %s", e)
# Check for config migrations
print()

View file

@ -12,11 +12,14 @@ Guides users through:
Config files are stored in ~/.hermes/ for easy access.
"""
import logging
import os
import sys
from pathlib import Path
from typing import Optional, Dict, Any
logger = logging.getLogger(__name__)
PROJECT_ROOT = Path(__file__).parent.parent.resolve()
# Import config helpers
@ -488,8 +491,8 @@ def run_setup_wizard(args):
inference_base_url=creds.get("base_url", ""),
api_key=creds.get("api_key", ""),
)
except Exception:
pass
except Exception as e:
logger.debug("Could not fetch Nous models after login: %s", e)
except SystemExit:
print_warning("Nous Portal login was cancelled or failed.")