from fastapi import Request, HTTPException, status
from fastapi.responses import JSONResponse
from sqlalchemy.exc import SQLAlchemyError
import logging

logger = logging.getLogger(__name__)

GENERIC_DB_ERROR = "Database error, please try again."
GENERIC_SERVER_ERROR = "Internal server error. Please contact support if the problem persists."

async def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError):
    logger.error("SQLAlchemyError", exc_info=exc)
    return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": GENERIC_DB_ERROR})

async def generic_exception_handler(request: Request, exc: Exception):
    logger.error("Unhandled exception", exc_info=exc)
    return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": GENERIC_SERVER_ERROR}) 