Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/v2/store/db.py (Python)

Code before:
        DatabaseError: If there's an issue updating or creating the profile
    """
    logger.info("Updating profile for user %s with data: %s", user_id, profile)
    try:
        # Sanitize username to allow only letters, numbers, and hyphens
        username = "".join(
            c if c.isalpha() or c == "-" or c.isnumeric() else ""
            for c in profile.username
        ).lower()
        # Check if profile exists for the given user_id
        existing_profile = await prisma.models.Profile.prisma().find_first(
            where={"userId": user_id}
        )
        if not existing_profile:
            raise backend.server.v2.store.exceptions.ProfileNotFoundError(
                f"Profile not found for user {user_id}. This should not be possible."
            )

        # Verify that the user is authorized to update this profile
        if existing_profile.userId != user_id:
            logger.error(
                "Unauthorized update attempt for profile %s by user %s",
                existing_profile.userId,
                user_id,
            )
            raise fastapi.HTTPException(
                status_code=403, detail="Unauthorized to update this profile"
            )
        logger.debug("Updating existing profile for user %s", user_id)
        # Prepare update data, only including non-None values
        update_data = {}
        if profile.name is not None:
            update_data["name"] = profile.name
        if profile.username is not None:
            update_data["username"] = username
        if profile.description is not None:
            update_data["description"] = profile.description
        if profile.links is not None:
            update_data["links"] = profile.links
        if profile.avatar_url is not None:
            update_data["avatarUrl"] = profile.avatar_url

        # Update the existing profile
        updated_profile = await prisma.models.Profile.prisma().update(
            where={"id": existing_profile.id},
            data=prisma.types.ProfileUpdateInput(**update_data),
        )
        if updated_profile is None:
            logger.error("Failed to update profile for user %s", user_id)
            raise backend.server.v2.store.exceptions.DatabaseError(
                "Failed to update profile"

Review comment (bug):
I don't think we should be raising HTTPExceptions here, in the DB layer. Can you do another error here?

Code after:
    """
    logger.info("Updating profile for user %s with data: %s", user_id, profile)
    try:
        # Sanitize username to allow only letters, numbers, and hyphens
        username = "".join(
            c if c.isalpha() or c == "-" or c.isnumeric() else ""
            for c in profile.username
        ).lower()
        # Check if profile exists for the given user_id
        existing_profile = await prisma.models.Profile.prisma().find_first(
            where={"userId": user_id}
        )
        if not existing_profile:
            raise backend.server.v2.store.exceptions.ProfileNotFoundError(
                f"Profile not found for user {user_id}. This should not be possible."
            )

        # Verify that the user is authorized to update this profile
        if existing_profile.userId != user_id:
            logger.error(
                "Unauthorized update attempt for profile %s by user %s",
                existing_profile.userId,
                user_id,
            )
            raise backend.server.v2.store.exceptions.DatabaseError(
                f"Unauthorized update attempt for profile {existing_profile.id} by user {user_id}"
            )

        logger.debug("Updating existing profile for user %s", user_id)
        # Prepare update data, only including non-None values
        update_data = {}
        if profile.name is not None:
            update_data["name"] = profile.name
        if profile.username is not None:
            update_data["username"] = username
        if profile.description is not None:
            update_data["description"] = profile.description
        if profile.links is not None:
            update_data["links"] = profile.links
        if profile.avatar_url is not None:
            update_data["avatarUrl"] = profile.avatar_url

        # Update the existing profile
        updated_profile = await prisma.models.Profile.prisma().update(
            where={"id": existing_profile.id},
            data=prisma.types.ProfileUpdateInput(**update_data),
        )
        if updated_profile is None:
            logger.error("Failed to update profile for user %s", user_id)
            raise backend.server.v2.store.exceptions.DatabaseError(
                "Failed to update profile"