forked from MemTensor/MemOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
53 lines (44 loc) · 1.75 KB
/
exceptions.py
File metadata and controls
53 lines (44 loc) · 1.75 KB
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
import logging
from fastapi.exceptions import HTTPException, RequestValidationError
from fastapi.requests import Request
from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
class APIExceptionHandler:
"""Centralized exception handling for MemOS APIs."""
@staticmethod
async def validation_error_handler(request: Request, exc: RequestValidationError):
"""Handle request validation errors."""
logger.error(f"Validation error: {exc.errors()}")
return JSONResponse(
status_code=422,
content={
"code": 422,
"message": "Parameter validation error",
"detail": exc.errors(),
"data": None,
},
)
@staticmethod
async def value_error_handler(request: Request, exc: ValueError):
"""Handle ValueError exceptions globally."""
logger.error(f"ValueError: {exc}")
return JSONResponse(
status_code=400,
content={"code": 400, "message": str(exc), "data": None},
)
@staticmethod
async def global_exception_handler(request: Request, exc: Exception):
"""Handle all unhandled exceptions globally."""
logger.error(f"Exception: {exc}")
return JSONResponse(
status_code=500,
content={"code": 500, "message": str(exc), "data": None},
)
@staticmethod
async def http_error_handler(request: Request, exc: HTTPException):
"""Handle HTTP exceptions globally."""
logger.error(f"HTTP error {exc.status_code}: {exc.detail}")
return JSONResponse(
status_code=exc.status_code,
content={"code": exc.status_code, "message": str(exc.detail), "data": None},
)