"""Concrete error classes for common failure scenarios in library ecosystems"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/core/errors.ipynb.

# %% auto 0
__all__ = ['ValidationError', 'ConfigurationError', 'ResourceError', 'PluginError', 'WorkerError']

# %% ../../nbs/core/errors.ipynb 3
from typing import Optional, Dict, Any
from .base import BaseError, ErrorContext, ErrorSeverity

# %% ../../nbs/core/errors.ipynb 6
class ValidationError(BaseError):
    """
    Raised when validation fails.
    
    Use for:
    - JSON schema validation failures
    - Resource availability validation (GPU busy, insufficient memory)
    - Input constraint violations
    - Configuration value validation
    
    Examples:
        - Plugin config doesn't match schema
        - GPU memory requirements exceed available resources
        - Invalid parameter values
    """
    
    def __init__(
        self,
        message: str,
        debug_info: Optional[str] = None,
        context: Optional[ErrorContext] = None,
        severity: ErrorSeverity = ErrorSeverity.ERROR,
        is_retryable: bool = False,  # Validation errors typically need fixes, not retries
        cause: Optional[Exception] = None,
        validation_errors: Optional[Dict[str, Any]] = None  # Structured validation details
    ):
        super().__init__(message, debug_info, context, severity, is_retryable, cause)
        self.validation_errors = validation_errors or {}
    
    def to_dict(self) -> Dict[str, Any]:
        result = super().to_dict()
        if self.validation_errors:
            result['validation_errors'] = self.validation_errors
        return result

# %% ../../nbs/core/errors.ipynb 11
class ConfigurationError(BaseError):
    """
    Raised when configuration operations fail.
    
    Use for:
    - Missing configuration files
    - Configuration file parse errors
    - Failed to save configuration
    - Invalid configuration structure
    
    Examples:
        - config.json not found
        - Malformed JSON in config file
        - Permission denied writing config
    """
    
    def __init__(
        self,
        message: str,
        debug_info: Optional[str] = None,
        context: Optional[ErrorContext] = None,
        severity: ErrorSeverity = ErrorSeverity.ERROR,
        is_retryable: bool = False,  # Config issues usually need manual fixes
        cause: Optional[Exception] = None,
        config_path: Optional[str] = None  # Path to problematic config file
    ):
        super().__init__(message, debug_info, context, severity, is_retryable, cause)
        self.config_path = config_path
    
    def to_dict(self) -> Dict[str, Any]:
        result = super().to_dict()
        if self.config_path:
            result['config_path'] = self.config_path
        return result

# %% ../../nbs/core/errors.ipynb 16
class ResourceError(BaseError):
    """
    Raised when resource conflicts or unavailability prevent operation.
    
    Use for:
    - GPU busy or unavailable
    - Insufficient system memory
    - Resource locked by another process
    - Quota exceeded
    
    Examples:
        - GPU in use by another worker
        - Out of memory error
        - External process using GPU
    """
    
    def __init__(
        self,
        message: str,
        debug_info: Optional[str] = None,
        context: Optional[ErrorContext] = None,
        severity: ErrorSeverity = ErrorSeverity.WARNING,  # Often transient
        is_retryable: bool = True,  # Resource conflicts may be temporary
        cause: Optional[Exception] = None,
        resource_type: Optional[str] = None,  # "GPU", "Memory", "Disk", etc.
        suggested_action: Optional[str] = None  # Guidance for resolution
    ):
        super().__init__(message, debug_info, context, severity, is_retryable, cause)
        self.resource_type = resource_type
        self.suggested_action = suggested_action
    
    def to_dict(self) -> Dict[str, Any]:
        result = super().to_dict()
        if self.resource_type:
            result['resource_type'] = self.resource_type
        if self.suggested_action:
            result['suggested_action'] = self.suggested_action
        return result

# %% ../../nbs/core/errors.ipynb 21
class PluginError(BaseError):
    """
    Raised when plugin operations fail.
    
    Use for:
    - Plugin not found or unavailable
    - Plugin initialization failures
    - Plugin execution errors
    - Missing plugin dependencies
    
    Examples:
        - Plugin entry point not found
        - Plugin initialization failed
        - Plugin execute() raised an error
        - Required dependencies not installed
    """
    
    def __init__(
        self,
        message: str,
        debug_info: Optional[str] = None,
        context: Optional[ErrorContext] = None,
        severity: ErrorSeverity = ErrorSeverity.ERROR,
        is_retryable: bool = False,  # Plugin errors usually need fixes
        cause: Optional[Exception] = None,
        plugin_id: Optional[str] = None,  # ID of problematic plugin
        plugin_name: Optional[str] = None  # Name of problematic plugin
    ):
        super().__init__(message, debug_info, context, severity, is_retryable, cause)
        self.plugin_id = plugin_id
        self.plugin_name = plugin_name
    
    def to_dict(self) -> Dict[str, Any]:
        result = super().to_dict()
        if self.plugin_id:
            result['plugin_id'] = self.plugin_id
        if self.plugin_name:
            result['plugin_name'] = self.plugin_name
        return result

# %% ../../nbs/core/errors.ipynb 26
class WorkerError(BaseError):
    """
    Raised when worker process operations fail.
    
    Use for:
    - Worker process crashes
    - Worker communication failures
    - Job execution failures
    - Worker timeout errors
    
    Examples:
        - Worker process terminated unexpectedly
        - Queue communication timeout
        - Job failed in worker
        - Worker failed to start
    """
    
    def __init__(
        self,
        message: str,
        debug_info: Optional[str] = None,
        context: Optional[ErrorContext] = None,
        severity: ErrorSeverity = ErrorSeverity.ERROR,
        is_retryable: bool = True,  # Worker errors may be transient
        cause: Optional[Exception] = None,
        worker_type: Optional[str] = None,  # "transcription", "llm", etc.
        job_id: Optional[str] = None  # Job that failed
    ):
        super().__init__(message, debug_info, context, severity, is_retryable, cause)
        self.worker_type = worker_type
        self.job_id = job_id
    
    def to_dict(self) -> Dict[str, Any]:
        result = super().to_dict()
        if self.worker_type:
            result['worker_type'] = self.worker_type
        if self.job_id:
            result['job_id'] = self.job_id
        return result
