diff --git a/be/app/config.py b/be/app/config.py index b5ef157..ffa6e0d 100644 --- a/be/app/config.py +++ b/be/app/config.py @@ -98,6 +98,12 @@ Organic Bananas HTTP_500_DETAIL: str = "Internal Server Error" HTTP_503_DETAIL: str = "Service Unavailable" + # --- Database Error Messages --- + DB_CONNECTION_ERROR: str = "Database connection error" + DB_INTEGRITY_ERROR: str = "Database integrity error" + DB_TRANSACTION_ERROR: str = "Database transaction error" + DB_QUERY_ERROR: str = "Database query error" + class Config: env_file = ".env" env_file_encoding = 'utf-8' diff --git a/be/app/core/exceptions.py b/be/app/core/exceptions.py index dd7c303..7d34b38 100644 --- a/be/app/core/exceptions.py +++ b/be/app/core/exceptions.py @@ -122,10 +122,10 @@ class DatabaseTransactionError(HTTPException): class DatabaseQueryError(HTTPException): """Raised when a database query fails.""" - def __init__(self): + def __init__(self, detail: str = settings.DB_QUERY_ERROR): super().__init__( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=settings.DB_QUERY_ERROR + detail=detail ) class OCRServiceUnavailableError(HTTPException): diff --git a/be/app/crud/list.py b/be/app/crud/list.py index b674563..f289662 100644 --- a/be/app/crud/list.py +++ b/be/app/crud/list.py @@ -51,18 +51,22 @@ async def get_lists_for_user(db: AsyncSession, user_id: int) -> PyList[ListModel ) user_group_ids = group_ids_result.scalars().all() - query = select(ListModel).where( - or_( - and_(ListModel.created_by_id == user_id, ListModel.group_id == None), - ListModel.group_id.in_(user_group_ids) - ) - ).order_by(ListModel.updated_at.desc()) + # Build conditions for the OR clause dynamically + conditions = [ + and_(ListModel.created_by_id == user_id, ListModel.group_id.is_(None)) + ] + if user_group_ids: # Only add the IN clause if there are group IDs + conditions.append(ListModel.group_id.in_(user_group_ids)) + + query = select(ListModel).where(or_(*conditions)).order_by(ListModel.updated_at.desc()) result = await db.execute(query) return result.scalars().all() except OperationalError as e: raise DatabaseConnectionError(f"Failed to connect to database: {str(e)}") except SQLAlchemyError as e: + # It would be helpful to log the original error e here for more detailed debugging + # For example: logger.error(f"SQLAlchemyError in get_lists_for_user: {type(e).__name__} - {str(e)}") raise DatabaseQueryError(f"Failed to query user lists: {str(e)}") async def get_list_by_id(db: AsyncSession, list_id: int, load_items: bool = False) -> Optional[ListModel]: