Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -17,9 +17,8 @@ import mimetypes
|
|
17 |
import uuid
|
18 |
import traceback
|
19 |
from datetime import datetime
|
20 |
-
|
21 |
-
# Pydantic
|
22 |
from pydantic import ValidationError
|
|
|
23 |
|
24 |
# Project imports
|
25 |
from websocket_handler import websocket_endpoint
|
@@ -35,14 +34,14 @@ from logger import log_error, log_info, log_warning
|
|
35 |
# Exception imports
|
36 |
from exceptions import (
|
37 |
DuplicateResourceError,
|
38 |
-
ValidationError,
|
39 |
-
ResourceNotFoundError,
|
40 |
-
FlareException,
|
41 |
RaceConditionError,
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
get_http_status_code
|
44 |
)
|
45 |
-
from dotenv import load_dotenv
|
46 |
|
47 |
# Load .env file if exists
|
48 |
load_dotenv()
|
@@ -152,7 +151,8 @@ app.include_router(chat_router, prefix="/api")
|
|
152 |
# ---------------- Admin API routes ----------------------------------
|
153 |
app.include_router(admin_router, prefix="/api/admin")
|
154 |
|
155 |
-
#
|
|
|
156 |
@app.exception_handler(Exception)
|
157 |
async def global_exception_handler(request: Request, exc: Exception):
|
158 |
"""Handle all unhandled exceptions"""
|
@@ -194,6 +194,7 @@ async def global_exception_handler(request: Request, exc: Exception):
|
|
194 |
})
|
195 |
)
|
196 |
|
|
|
197 |
@app.exception_handler(DuplicateResourceError)
|
198 |
async def duplicate_resource_handler(request: Request, exc: DuplicateResourceError):
|
199 |
"""Handle duplicate resource errors"""
|
@@ -240,6 +241,40 @@ async def resource_not_found_handler(request: Request, exc: ResourceNotFoundErro
|
|
240 |
}
|
241 |
)
|
242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
# ---------------- Metrics endpoint -----------------
|
244 |
@app.get("/metrics")
|
245 |
async def get_metrics():
|
|
|
17 |
import uuid
|
18 |
import traceback
|
19 |
from datetime import datetime
|
|
|
|
|
20 |
from pydantic import ValidationError
|
21 |
+
from dotenv import load_dotenv
|
22 |
|
23 |
# Project imports
|
24 |
from websocket_handler import websocket_endpoint
|
|
|
34 |
# Exception imports
|
35 |
from exceptions import (
|
36 |
DuplicateResourceError,
|
|
|
|
|
|
|
37 |
RaceConditionError,
|
38 |
+
ValidationError,
|
39 |
+
ResourceNotFoundError,
|
40 |
+
AuthenticationError,
|
41 |
+
AuthorizationError,
|
42 |
+
ConfigurationError,
|
43 |
get_http_status_code
|
44 |
)
|
|
|
45 |
|
46 |
# Load .env file if exists
|
47 |
load_dotenv()
|
|
|
151 |
# ---------------- Admin API routes ----------------------------------
|
152 |
app.include_router(admin_router, prefix="/api/admin")
|
153 |
|
154 |
+
# ---------------- Exception Handlers ----------------------------------
|
155 |
+
# Add global exception handler
|
156 |
@app.exception_handler(Exception)
|
157 |
async def global_exception_handler(request: Request, exc: Exception):
|
158 |
"""Handle all unhandled exceptions"""
|
|
|
194 |
})
|
195 |
)
|
196 |
|
197 |
+
# Add custom exception handlers
|
198 |
@app.exception_handler(DuplicateResourceError)
|
199 |
async def duplicate_resource_handler(request: Request, exc: DuplicateResourceError):
|
200 |
"""Handle duplicate resource errors"""
|
|
|
241 |
}
|
242 |
)
|
243 |
|
244 |
+
@app.exception_handler(AuthenticationError)
|
245 |
+
async def authentication_error_handler(request: Request, exc: AuthenticationError):
|
246 |
+
"""Handle authentication errors"""
|
247 |
+
return JSONResponse(
|
248 |
+
status_code=401,
|
249 |
+
content={
|
250 |
+
"detail": str(exc),
|
251 |
+
"error_type": "authentication_error"
|
252 |
+
}
|
253 |
+
)
|
254 |
+
|
255 |
+
@app.exception_handler(AuthorizationError)
|
256 |
+
async def authorization_error_handler(request: Request, exc: AuthorizationError):
|
257 |
+
"""Handle authorization errors"""
|
258 |
+
return JSONResponse(
|
259 |
+
status_code=403,
|
260 |
+
content={
|
261 |
+
"detail": str(exc),
|
262 |
+
"error_type": "authorization_error"
|
263 |
+
}
|
264 |
+
)
|
265 |
+
|
266 |
+
@app.exception_handler(ConfigurationError)
|
267 |
+
async def configuration_error_handler(request: Request, exc: ConfigurationError):
|
268 |
+
"""Handle configuration errors"""
|
269 |
+
return JSONResponse(
|
270 |
+
status_code=500,
|
271 |
+
content={
|
272 |
+
"detail": str(exc),
|
273 |
+
"error_type": "configuration_error",
|
274 |
+
"config_key": exc.details.get("config_key")
|
275 |
+
}
|
276 |
+
)
|
277 |
+
|
278 |
# ---------------- Metrics endpoint -----------------
|
279 |
@app.get("/metrics")
|
280 |
async def get_metrics():
|