Files
Pygentic-AI/src
Francis Secada 018a47f5d9
Some checks failed
Bandit / bandit (push) Has been cancelled
Docker Image CI / build (3.13) (push) Has been cancelled
Tests / test (3.13) (push) Has been cancelled
refactor: standardize error handling and fix Reddit API pattern
## 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>
2026-02-04 21:52:44 -05:00
..
2025-01-16 11:33:26 -05:00