File size: 6,721 Bytes
8cbe6eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"""
Custom exceptions for the voice control optimization system.

This module defines all custom exceptions used throughout the voice control
system to provide clear error handling and debugging information.
"""


class VoiceControlError(Exception):
    """Base exception for all voice control system errors."""
    
    def __init__(self, message: str, error_code: str = None, details: dict = None):
        super().__init__(message)
        self.message = message
        self.error_code = error_code or self.__class__.__name__
        self.details = details or {}


class ProviderError(VoiceControlError):
    """Base exception for STT provider errors."""
    
    def __init__(self, provider: str, message: str, error_code: str = None, details: dict = None):
        super().__init__(message, error_code, details)
        self.provider = provider


class ProviderUnavailableError(ProviderError):
    """Raised when a provider is unavailable or unhealthy."""
    pass


class ProviderTimeoutError(ProviderError):
    """Raised when a provider request times out."""
    pass


class ProviderAuthenticationError(ProviderError):
    """Raised when provider authentication fails."""
    pass


class QuotaExceededError(ProviderError):
    """Raised when provider quota limits are exceeded."""
    
    def __init__(self, provider: str, quota_type: str, current_usage: float, limit: float):
        message = f"Quota exceeded for {provider}: {quota_type} usage {current_usage} >= limit {limit}"
        super().__init__(provider, message)
        self.quota_type = quota_type
        self.current_usage = current_usage
        self.limit = limit


class RateLimitExceededError(ProviderError):
    """Raised when provider rate limits are exceeded."""
    
    def __init__(self, provider: str, quota_type: str = None, retry_after: float = None):
        message = f"Rate limit exceeded for {provider}"
        if quota_type:
            message += f" ({quota_type})"
        if retry_after:
            message += f", retry after {retry_after:.1f} seconds"
        super().__init__(provider, message)
        self.quota_type = quota_type
        self.retry_after = retry_after


class UnsupportedFormatError(ProviderError):
    """Raised when a provider doesn't support the requested audio format."""
    
    def __init__(self, provider: str, format: str, supported_formats: list = None):
        message = f"Provider {provider} does not support format '{format}'"
        if supported_formats:
            message += f", supported formats: {', '.join(supported_formats)}"
        super().__init__(provider, message)
        self.format = format
        self.supported_formats = supported_formats or []


class UnsupportedLanguageError(ProviderError):
    """Raised when a provider doesn't support the requested language."""
    
    def __init__(self, provider: str, language: str, supported_languages: list = None):
        message = f"Provider {provider} does not support language '{language}'"
        if supported_languages:
            message += f", supported languages: {', '.join(supported_languages)}"
        super().__init__(provider, message)
        self.language = language
        self.supported_languages = supported_languages or []


class AudioProcessingError(VoiceControlError):
    """Raised when audio processing operations fail."""
    
    def __init__(self, operation: str, message: str, details: dict = None):
        super().__init__(f"Audio processing failed during {operation}: {message}", details=details)
        self.operation = operation


class AudioFormatError(AudioProcessingError):
    """Raised when audio format conversion fails."""
    
    def __init__(self, source_format: str, target_format: str, message: str):
        super().__init__(
            "format_conversion",
            f"Failed to convert from {source_format} to {target_format}: {message}"
        )
        self.source_format = source_format
        self.target_format = target_format


class CacheError(VoiceControlError):
    """Base exception for cache-related errors."""
    pass


class CacheKeyError(CacheError):
    """Raised when cache key generation fails."""
    pass


class CacheStorageError(CacheError):
    """Raised when cache storage operations fail."""
    pass


class ConfigurationError(VoiceControlError):
    """Raised when configuration is invalid or missing."""
    
    def __init__(self, config_key: str, message: str, details: dict = None):
        super().__init__(f"Configuration error for '{config_key}': {message}", details=details)
        self.config_key = config_key


class ValidationError(VoiceControlError):
    """Raised when data validation fails."""
    
    def __init__(self, field: str, value: any, message: str):
        super().__init__(f"Validation failed for field '{field}' with value '{value}': {message}")
        self.field = field
        self.value = value


class CircuitBreakerError(ProviderError):
    """Raised when a provider's circuit breaker is open."""
    
    def __init__(self, provider: str, failure_count: int, threshold: int):
        message = f"Circuit breaker open for {provider}: {failure_count} failures >= {threshold} threshold"
        super().__init__(provider, message)
        self.failure_count = failure_count
        self.threshold = threshold


class NoProvidersAvailableError(VoiceControlError):
    """Raised when no providers are available for transcription."""
    
    def __init__(self, attempted_providers: list = None):
        message = "No providers available for transcription"
        if attempted_providers:
            message += f", attempted: {', '.join(attempted_providers)}"
        super().__init__(message)
        self.attempted_providers = attempted_providers or []


class TranscriptionError(VoiceControlError):
    """Raised when transcription fails for non-provider-specific reasons."""
    
    def __init__(self, message: str, confidence: float = None, partial_result: str = None):
        super().__init__(message)
        self.confidence = confidence
        self.partial_result = partial_result


class CostMonitoringError(VoiceControlError):
    """Raised when cost monitoring operations fail."""
    pass


class BudgetExceededError(CostMonitoringError):
    """Raised when budget thresholds are exceeded."""
    
    def __init__(self, provider: str, current_cost: float, threshold: float, time_period: str):
        message = f"Budget exceeded for {provider}: ${current_cost:.2f} >= ${threshold:.2f} for {time_period}"
        super().__init__(message)
        self.provider = provider
        self.current_cost = current_cost
        self.threshold = threshold
        self.time_period = time_period