mirror of
https://github.com/fsecada01/Pygentic-AI.git
synced 2026-05-12 20:25:04 +00:00
## Error Handling Improvements **Created Custom Exception Hierarchy** - New module: backend/core/exceptions.py - Base: ToolError (with tool name, message, original error) - Specific: NetworkError, APIError, ValidationError, ContentError **Standardized All Tools** 1. **fetch_website_content** - Separate handling for TimeoutException vs HTTPError - Raises NetworkError for network issues - Raises ContentError for parsing failures - Added debug logging for success cases 2. **search_web** - Added comprehensive error handling (was missing) - Raises APIError on Tavily failures - Added debug logging for result counts 3. **analyze_competition** - Changed from returning error strings to raising APIError - Proper exception chaining with "raise from" - Check for uninitialized client upfront - Added debug logging 4. **get_reddit_insights** - Added comprehensive error handling (was missing) - Raises APIError on Reddit failures - Check for uninitialized client upfront ## Reddit API Pattern Fix (#25) **Problem**: Inconsistent sync/async logic - Used sync loop for ≤3 subreddits - Switched to async for >3 subreddits - Confusing and inefficient **Solution**: Always use async pattern - Consistent async execution for all counts - Concurrent searches with asyncio.gather - Handle exceptions per-subreddit with return_exceptions=True - Added limit=10 to prevent excessive results - Removed unnecessary event loop manipulation ## Benefits - **Consistent error handling** across all tools - **Better error messages** with context (tool name, original error) - **Proper exception chaining** for debugging - **Improved performance** for Reddit searches - **Better resilience** - continues on partial failures - **Enhanced logging** - debug logs for success, warnings for failures ## Impact - More predictable error behavior - Easier debugging with structured exceptions - Better performance for Reddit API usage - Cleaner, more maintainable code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>