Skip to content

Conversation

@1234-ad
Copy link

@1234-ad 1234-ad commented Jan 12, 2026

Summary

Adds comprehensive error handling infrastructure with custom exceptions, centralized logging, and retry mechanisms for resilient API calls.

Changes

pr_review/exceptions.py

Custom exception hierarchy:

  • PRReviewError: Base exception for all PR review errors
  • GitHubAPIError: GitHub API failures with status code tracking
  • MetricsCalculationError: Metrics computation failures
  • AILabelingError: AI labeling service failures
  • ReviewerAssignmentError: Reviewer assignment failures
  • DiscordNotificationError: Discord notification failures
  • ConfigurationError: Configuration validation errors
  • ValidationError: Input validation errors

pr_review/logging_config.py

Centralized logging utilities:

  • setup_logging(): Configure logging with console and file output
  • Rotating file handler with configurable size and backup count
  • LoggerMixin: Add logging to any class via inheritance
  • LogLevel: Context manager for temporary log level changes
  • Structured log format with filename and line numbers

pr_review/utils/retry_utils.py

Retry and resilience utilities:

  • @retry: Decorator for sync functions with exponential backoff
  • @retry_async: Decorator for async functions with exponential backoff
  • CircuitBreaker: Prevent cascading failures with circuit breaker pattern
  • Configurable retry attempts, delays, and backoff multipliers
  • Custom exception handling and retry callbacks

Benefits

  • ✅ Better error categorization and handling
  • ✅ Easier debugging with specific exception types
  • ✅ Consistent logging across all modules
  • ✅ Resilient API calls that handle transient failures
  • ✅ Exponential backoff prevents overwhelming failing services
  • ✅ Circuit breaker prevents cascading failures
  • ✅ Production-ready reliability patterns
  • ✅ Improved error messages and context

Usage Examples

# Custom exceptions
from exceptions import GitHubAPIError
raise GitHubAPIError("API rate limit exceeded", status_code=429)

# Logging
from logging_config import setup_logging, LoggerMixin
logger = setup_logging('my_module', level='DEBUG', log_file='logs/app.log')

# Retry decorator
from utils.retry_utils import retry
@retry(max_attempts=3, delay=1.0, backoff=2.0)
def fetch_github_data():
    return github_client.get_pull_request(repo, pr_number)

# Circuit breaker
breaker = CircuitBreaker(failure_threshold=5, recovery_timeout=60.0)
result = breaker.call(risky_api_call, arg1, arg2)

Testing

  • All utilities tested with Python 3.13
  • Exception hierarchy validated
  • Logging configuration tested with file rotation
  • Retry logic tested with mock failures

Impact

  • Significantly improves error handling reliability
  • Better debugging and troubleshooting capabilities
  • More resilient to transient failures
  • Production-ready error handling patterns

Add comprehensive custom exception hierarchy:
- PRReviewError: Base exception for all PR review errors
- GitHubAPIError: GitHub API failures with status code tracking
- MetricsCalculationError: Metrics computation failures
- AILabelingError: AI labeling service failures
- ReviewerAssignmentError: Reviewer assignment failures
- DiscordNotificationError: Discord notification failures
- ConfigurationError: Configuration validation errors
- ValidationError: Input validation errors

Benefits:
- Better error categorization and handling
- Easier debugging with specific exception types
- Improved error messages and context
- Enables targeted exception handling in try-except blocks
Add comprehensive logging utilities:
- setup_logging(): Configure logging with console and file output
- Rotating file handler with configurable size and backup count
- LoggerMixin: Add logging to any class via inheritance
- LogLevel: Context manager for temporary log level changes
- Structured log format with filename and line numbers
- Configurable log levels and custom format strings

Benefits:
- Consistent logging across all modules
- Easy log file rotation to prevent disk space issues
- Better debugging with file/line information
- Flexible logging configuration
- Production-ready logging setup

Usage:
    from logging_config import setup_logging, LoggerMixin
    
    logger = setup_logging('my_module', level='DEBUG', log_file='logs/app.log')
    logger.info('Application started')
Add comprehensive retry and resilience utilities:
- retry(): Decorator for sync functions with exponential backoff
- retry_async(): Decorator for async functions with exponential backoff
- CircuitBreaker: Prevent cascading failures with circuit breaker pattern
- Configurable retry attempts, delays, and backoff multipliers
- Custom exception handling and retry callbacks
- Detailed logging of retry attempts and failures

Benefits:
- Resilient API calls that handle transient failures
- Exponential backoff prevents overwhelming failing services
- Circuit breaker prevents cascading failures
- Better error handling and recovery
- Production-ready reliability patterns

Usage:
    from retry_utils import retry, CircuitBreaker
    
    @Retry(max_attempts=3, delay=1.0, backoff=2.0)
    def fetch_github_data():
        return github_client.get_pull_request(repo, pr_number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant