""" Error handling and retry mechanisms for DICOM operations. """ import time import functools from retry import retry from utils.logger import main_logger as logger from config import settings # Define custom exceptions class DicomConnectionError(Exception): """Error when failing to establish DICOM association.""" pass class DicomQueryError(Exception): """Error when C-FIND operation fails.""" pass class DicomRetrieveError(Exception): """Error when C-GET operation fails.""" pass class DicomStoreError(Exception): """Error when C-STORE operation fails.""" pass def dicom_retry(exception_types=(Exception,)): """ Decorator to retry DICOM operations with exponential backoff. Args: exception_types: Tuple of exception types to retry on. Returns: Decorated function with retry logic. """ def decorator(func): @functools.wraps(func) @retry( exceptions=exception_types, tries=settings.MAX_RETRIES, delay=settings.RETRY_DELAY, backoff=2, logger=logger ) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except exception_types as e: logger.error(f"Error in {func.__name__}: {str(e)}") raise return wrapper return decorator