File size: 1,746 Bytes
93bd7fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | """Custom exceptions for AnkiGen application.
This module provides a hierarchy of custom exceptions to standardize
error handling across the codebase.
"""
class AnkigenError(Exception):
"""Base exception for all AnkiGen errors."""
pass
class ValidationError(AnkigenError):
"""Raised when input validation fails."""
pass
class SecurityError(AnkigenError):
"""Raised when a security check fails (SSRF, command injection, etc.)."""
pass
class APIError(AnkigenError):
"""Base exception for API-related errors."""
pass
class OpenAIAPIError(APIError):
"""Raised when OpenAI API calls fail."""
pass
class Context7APIError(APIError):
"""Raised when Context7 API calls fail."""
pass
class ExportError(AnkigenError):
"""Base exception for export-related errors."""
pass
class CardGenerationError(AnkigenError):
"""Raised when card generation fails."""
pass
class ConfigurationError(AnkigenError):
"""Raised when configuration is invalid or missing."""
pass
def handle_exception(
exc: Exception,
logger,
message: str,
reraise: bool = True,
reraise_as: type[Exception] | None = None,
) -> None:
"""Standardized exception handler.
Args:
exc: The exception to handle
logger: Logger instance to use
message: Error message to log
reraise: Whether to re-raise the exception
reraise_as: Optional exception type to wrap and re-raise as
Raises:
The original exception or wrapped exception if reraise is True
"""
logger.error(f"{message}: {exc}", exc_info=True)
if reraise:
if reraise_as:
raise reraise_as(f"{message}: {exc}") from exc
raise
|