2025-12-15 17:18:27 +03:00

37 lines
1.1 KiB
Python

"""
Обработчик ошибок для FastAPI
"""
from fastapi import Request, status
from fastapi.responses import JSONResponse
from src.shared.exceptions import (
LawyerAIException,
NotFoundError,
UnauthorizedError,
ForbiddenError,
ValidationError,
DatabaseError
)
async def exception_handler(request: Request, exc: LawyerAIException) -> JSONResponse:
"""Обработчик кастомных исключений"""
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
detail = str(exc)
if isinstance(exc, NotFoundError):
status_code = status.HTTP_404_NOT_FOUND
elif isinstance(exc, UnauthorizedError):
status_code = status.HTTP_401_UNAUTHORIZED
elif isinstance(exc, ForbiddenError):
status_code = status.HTTP_403_FORBIDDEN
elif isinstance(exc, ValidationError):
status_code = status.HTTP_400_BAD_REQUEST
elif isinstance(exc, DatabaseError):
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return JSONResponse(
status_code=status_code,
content={"detail": detail, "type": exc.__class__.__name__}
)