Spaces:
Building
Building
Delete exceptions.py
Browse files- exceptions.py +0 -193
exceptions.py
DELETED
@@ -1,193 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Custom Exception Classes for Flare Platform
|
3 |
-
"""
|
4 |
-
from typing import Optional, Dict, Any
|
5 |
-
from datetime import datetime
|
6 |
-
|
7 |
-
class FlareException(Exception):
|
8 |
-
"""Base exception for Flare"""
|
9 |
-
def __init__(self, message: str, details: Optional[Dict[str, Any]] = None):
|
10 |
-
self.message = message
|
11 |
-
self.details = details or {}
|
12 |
-
self.timestamp = datetime.utcnow()
|
13 |
-
super().__init__(self.message)
|
14 |
-
|
15 |
-
def to_dict(self) -> Dict[str, Any]:
|
16 |
-
"""Convert exception to dictionary"""
|
17 |
-
return {
|
18 |
-
"error": self.__class__.__name__,
|
19 |
-
"message": self.message,
|
20 |
-
"details": self.details,
|
21 |
-
"timestamp": self.timestamp.isoformat()
|
22 |
-
}
|
23 |
-
|
24 |
-
def to_http_detail(self) -> Dict[str, Any]:
|
25 |
-
"""Convert to HTTP response detail"""
|
26 |
-
return {
|
27 |
-
"detail": self.message,
|
28 |
-
"error_type": self.__class__.__name__.lower().replace('error', ''),
|
29 |
-
**self.details
|
30 |
-
}
|
31 |
-
|
32 |
-
class RaceConditionError(FlareException):
|
33 |
-
"""Raised when a race condition is detected during concurrent updates"""
|
34 |
-
def __init__(
|
35 |
-
self,
|
36 |
-
message: str,
|
37 |
-
current_user: Optional[str] = None,
|
38 |
-
last_update_user: Optional[str] = None,
|
39 |
-
last_update_date: Optional[str] = None,
|
40 |
-
entity_type: Optional[str] = None,
|
41 |
-
entity_id: Optional[Any] = None
|
42 |
-
):
|
43 |
-
details = {
|
44 |
-
"current_user": current_user,
|
45 |
-
"last_update_user": last_update_user,
|
46 |
-
"last_update_date": last_update_date,
|
47 |
-
"entity_type": entity_type,
|
48 |
-
"entity_id": entity_id,
|
49 |
-
"action": "Please reload the data and try again"
|
50 |
-
}
|
51 |
-
super().__init__(message, details)
|
52 |
-
self.current_user = current_user
|
53 |
-
self.last_update_user = last_update_user
|
54 |
-
self.last_update_date = last_update_date
|
55 |
-
|
56 |
-
def to_http_detail(self) -> Dict[str, Any]:
|
57 |
-
"""Convert to HTTPException detail format with proper serialization"""
|
58 |
-
return {
|
59 |
-
"message": self.message,
|
60 |
-
"last_update_user": self.last_update_user,
|
61 |
-
"last_update_date": self.last_update_date.isoformat() if isinstance(self.last_update_date, datetime) else self.last_update_date,
|
62 |
-
"type": "race_condition"
|
63 |
-
}
|
64 |
-
|
65 |
-
class ConfigurationError(FlareException):
|
66 |
-
"""Raised when there's a configuration issue"""
|
67 |
-
def __init__(self, message: str, config_key: Optional[str] = None):
|
68 |
-
details = {"config_key": config_key} if config_key else {}
|
69 |
-
super().__init__(message, details)
|
70 |
-
|
71 |
-
class ValidationError(FlareException):
|
72 |
-
"""Raised when validation fails"""
|
73 |
-
def __init__(self, message: str, field: Optional[str] = None, value: Any = None):
|
74 |
-
details = {}
|
75 |
-
if field:
|
76 |
-
details["field"] = field
|
77 |
-
if value is not None:
|
78 |
-
details["value"] = str(value)
|
79 |
-
super().__init__(message, details)
|
80 |
-
|
81 |
-
class AuthenticationError(FlareException):
|
82 |
-
"""Raised when authentication fails"""
|
83 |
-
def __init__(self, message: str = "Authentication failed"):
|
84 |
-
super().__init__(message)
|
85 |
-
|
86 |
-
class AuthorizationError(FlareException):
|
87 |
-
"""Raised when authorization fails"""
|
88 |
-
def __init__(self, message: str = "Insufficient permissions", required_permission: Optional[str] = None):
|
89 |
-
details = {"required_permission": required_permission} if required_permission else {}
|
90 |
-
super().__init__(message, details)
|
91 |
-
|
92 |
-
class SessionError(FlareException):
|
93 |
-
"""Raised when there's a session-related error"""
|
94 |
-
def __init__(self, message: str, session_id: Optional[str] = None):
|
95 |
-
details = {"session_id": session_id} if session_id else {}
|
96 |
-
super().__init__(message, details)
|
97 |
-
|
98 |
-
class ProviderError(FlareException):
|
99 |
-
"""Raised when a provider (LLM, TTS, STT) fails"""
|
100 |
-
def __init__(self, message: str, provider_type: str, provider_name: str, original_error: Optional[str] = None):
|
101 |
-
details = {
|
102 |
-
"provider_type": provider_type,
|
103 |
-
"provider_name": provider_name,
|
104 |
-
"original_error": original_error
|
105 |
-
}
|
106 |
-
super().__init__(message, details)
|
107 |
-
|
108 |
-
class APICallError(FlareException):
|
109 |
-
"""Raised when an external API call fails"""
|
110 |
-
def __init__(
|
111 |
-
self,
|
112 |
-
message: str,
|
113 |
-
api_name: str,
|
114 |
-
status_code: Optional[int] = None,
|
115 |
-
response_body: Optional[str] = None
|
116 |
-
):
|
117 |
-
details = {
|
118 |
-
"api_name": api_name,
|
119 |
-
"status_code": status_code,
|
120 |
-
"response_body": response_body
|
121 |
-
}
|
122 |
-
super().__init__(message, details)
|
123 |
-
|
124 |
-
class WebSocketError(FlareException):
|
125 |
-
"""Raised when WebSocket operations fail"""
|
126 |
-
def __init__(self, message: str, session_id: Optional[str] = None, state: Optional[str] = None):
|
127 |
-
details = {
|
128 |
-
"session_id": session_id,
|
129 |
-
"state": state
|
130 |
-
}
|
131 |
-
super().__init__(message, details)
|
132 |
-
|
133 |
-
class ResourceNotFoundError(FlareException):
|
134 |
-
"""Raised when a requested resource is not found"""
|
135 |
-
def __init__(self, resource_type: str, resource_id: Any):
|
136 |
-
message = f"{resource_type} not found: {resource_id}"
|
137 |
-
details = {
|
138 |
-
"resource_type": resource_type,
|
139 |
-
"resource_id": str(resource_id)
|
140 |
-
}
|
141 |
-
super().__init__(message, details)
|
142 |
-
|
143 |
-
class DuplicateResourceError(FlareException):
|
144 |
-
"""Raised when attempting to create a duplicate resource"""
|
145 |
-
def __init__(self, resource_type: str, identifier: str):
|
146 |
-
message = f"{resource_type} already exists: {identifier}"
|
147 |
-
details = {
|
148 |
-
"resource_type": resource_type,
|
149 |
-
"identifier": identifier
|
150 |
-
}
|
151 |
-
super().__init__(message, details)
|
152 |
-
|
153 |
-
# Error response formatters
|
154 |
-
def format_error_response(error: Exception, request_id: Optional[str] = None) -> Dict[str, Any]:
|
155 |
-
"""Format any exception into a standardized error response"""
|
156 |
-
if isinstance(error, FlareException):
|
157 |
-
response = error.to_dict()
|
158 |
-
else:
|
159 |
-
# Generic error
|
160 |
-
response = {
|
161 |
-
"error": error.__class__.__name__,
|
162 |
-
"message": str(error),
|
163 |
-
"details": {},
|
164 |
-
"timestamp": datetime.utcnow().isoformat()
|
165 |
-
}
|
166 |
-
|
167 |
-
if request_id:
|
168 |
-
response["request_id"] = request_id
|
169 |
-
|
170 |
-
return response
|
171 |
-
|
172 |
-
def get_http_status_code(error: Exception) -> int:
|
173 |
-
"""Get appropriate HTTP status code for an exception"""
|
174 |
-
status_map = {
|
175 |
-
ValidationError: 422,
|
176 |
-
AuthenticationError: 401,
|
177 |
-
AuthorizationError: 403,
|
178 |
-
ResourceNotFoundError: 404,
|
179 |
-
DuplicateResourceError: 409,
|
180 |
-
RaceConditionError: 409,
|
181 |
-
ConfigurationError: 500,
|
182 |
-
ProviderError: 503,
|
183 |
-
APICallError: 502,
|
184 |
-
WebSocketError: 500,
|
185 |
-
SessionError: 400
|
186 |
-
}
|
187 |
-
|
188 |
-
for error_class, status_code in status_map.items():
|
189 |
-
if isinstance(error, error_class):
|
190 |
-
return status_code
|
191 |
-
|
192 |
-
# Default to 500 for unknown errors
|
193 |
-
return 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|