Repository: ray-project/ray
File: python/ray/autoscaler/v2/instance_manager/cloud_providers/kuberay/cloud_provider.py (Python)

Code before:

        if scale_request.worker_groups_with_pending_deletes:
            error_msg = (
                "There are workers to be deleted from: "
                f"{scale_request.worker_groups_with_pending_deletes}. "
                "Waiting for them to be deleted before creating new workers."
            )
            logger.warning(error_msg)
            self._add_launch_errors(
                shape,
                request_id,
                details=error_msg,
            )
            return

        try:
            self._submit_scale_request(scale_request)
        except Exception as e:
            logger.exception(f"Error launching nodes: {scale_request}")
            self._add_launch_errors(shape, request_id, details=str(e), e=e)

    def poll_errors(self) -> List[CloudInstanceProviderError]:
        errors = []
        errors += self._launch_errors_queue
        errors += self._terminate_errors_queue
        self._launch_errors_queue = []
        self._terminate_errors_queue = []
        return errors

    ############################
    # Private
    ############################

    def _initialize_scale_request(
        self, to_launch: Dict[NodeType, int], to_delete_instances: List[CloudInstanceId]
    ) -> "KubeRayProvider.ScaleRequest":
        """
        Initialize the scale request based on the current state of the cluster and
        the desired state (to launch, to delete).

        Args:
            to_launch: The desired number of workers to launch for each node type.
            to_delete_instances: The instances to delete.

        Returns:
            The scale request.
        """

        # Update the cached states.
        self._sync_with_api_server()
        ray_cluster = self.ray_cluster

Review comment (suggestion):
```suggestion
            logger.exception(f"Error launching nodes: {scale_request}")
```

Code after:
        scale_request = None
        try:
            scale_request = self._initialize_scale_request(
                to_launch=shape, to_delete_instances=[]
            )

            if scale_request.worker_groups_with_pending_deletes:
                error_msg = (
                    "There are workers to be deleted from: "
                    f"{scale_request.worker_groups_with_pending_deletes}. "
                    "Waiting for them to be deleted before creating new workers."
                )
                logger.warning(error_msg)
                self._add_launch_errors(
                    shape,
                    request_id,
                    details=error_msg,
                )
                return

            self._submit_scale_request(scale_request)
            # Only add to processed requests if successful
            self._requests.add(request_id)

        except Exception as e:
            logger.exception(f"Error launching nodes: {scale_request or 'N/A'}")
            self._add_launch_errors(shape, request_id, details=str(e), e=e)

    def poll_errors(self) -> List[CloudInstanceProviderError]:
        errors = []
        errors += self._launch_errors_queue
        errors += self._terminate_errors_queue
        self._launch_errors_queue = []
        self._terminate_errors_queue = []
        return errors

    ############################
    # Private
    ############################

    def _initialize_scale_request(
        self, to_launch: Dict[NodeType, int], to_delete_instances: List[CloudInstanceId]
    ) -> "KubeRayProvider.ScaleRequest":
        """
        Initialize the scale request based on the current state of the cluster and
        the desired state (to launch, to delete).

        Args:
            to_launch: The desired number of workers to launch for each node type.
            to_delete_instances: The instances to delete.