Repository: astral-sh/uv
File: crates/uv-publish/src/lib.rs (Rust)

Code before:
        let result = request.send().await;
        let response = match result {
            Ok(response) => {
                // When the user accidentally uses https://test.pypi.org/legacy (no slash) as publish URL, we
                // get a redirect to https://test.pypi.org/legacy/ (the canonical index URL).
                // In the above case we get 308, reqwest or `RedirectClientWithMiddleware` would try
                // cloning the streaming body, which is not possible.
                // For https://test.pypi.org/simple (no slash), we get 301, which means we should make a GET request:
                // https://fetch.spec.whatwg.org/#http-redirect-fetch).
                // Reqwest doesn't support redirect policies conditional on the HTTP
                // method (https://github.com/seanmonstar/reqwest/issues/1777#issuecomment-2303386160), so we're
                // implementing our custom redirection logic.
                if response.status().is_redirection() {
                    if n_past_redirections >= max_redirects {
                        return Err(PublishError::PublishSend(
                            group.file.clone(),
                            current_registry.clone().into(),
                            PublishSendError::TooManyRedirectsError(
                                n_past_redirections,
                                max_redirects,
                            )
                            .into(),
                        ));
                    }
                    if let Some(location_header) = response.headers().get(LOCATION) {
                        let location = location_header.to_str().unwrap();
                        current_registry = DisplaySafeUrl::parse(location).unwrap();
                        warn!("Redirecting the request to: {}", current_registry);
                        n_past_redirections += 1;
                        continue;
                    }
                }
                reporter.on_upload_complete(idx);
                response
            }
            Err(err) => {
                if is_transient_network_error(&err) {
                    let retry_decision = retry_policy.should_retry(start_time, n_past_retries);
                    if let reqwest_retry::RetryDecision::Retry { execute_after } = retry_decision {
                        let duration = execute_after
                            .duration_since(SystemTime::now())
                            .unwrap_or_else(|_| Duration::default());
                        warn_user!(
                            "Transient failure while handling response for {}; retrying after {}s...",
                            current_registry,
                            duration.as_secs()
                        );
                        tokio::time::sleep(duration).await;
                        n_past_retries += 1;
                        continue;
                    }

Review comment (suggestion):
We can't unwrap here, we need to handle those errors gracefully. In uv production code, we need to avoid panics whenever possible; In this case, it's external input, so we have to assume it's in some way malformed.

Code after:
            credentials,
            form_metadata,
            reporter.clone(),
        )
        .await
        .map_err(|err| PublishError::PublishPrepare(group.file.clone(), Box::new(err)))?;

        let result = request.send().await;
        let response = match result {
            Ok(response) => {
                // When the user accidentally uses https://test.pypi.org/legacy (no slash) as publish URL, we
                // get a redirect to https://test.pypi.org/legacy/ (the canonical index URL).
                // In the above case we get 308, where reqwest or `RedirectClientWithMiddleware` would try
                // cloning the streaming body, which is not possible.
                // For https://test.pypi.org/simple (no slash), we get 301, which means we should make a GET request:
                // https://fetch.spec.whatwg.org/#http-redirect-fetch).
                // Reqwest doesn't support redirect policies conditional on the HTTP
                // method (https://github.com/seanmonstar/reqwest/issues/1777#issuecomment-2303386160), so we're
                // implementing our custom redirection logic.
                if response.status().is_redirection() {
                    if n_past_redirections >= max_redirects {
                        return Err(PublishError::PublishSend(
                            group.file.clone(),
                            current_registry.clone().into(),
                            PublishSendError::TooManyRedirects(n_past_redirections).into(),
                        ));
                    }
                    let location = response
                        .headers()
                        .get(LOCATION)
                        .ok_or_else(|| {
                            PublishError::PublishSend(
                                group.file.clone(),
                                current_registry.clone().into(),
                                PublishSendError::RedirectNoLocation.into(),
                            )
                        })?
                        .to_str()
                        .map_err(|err| {
                            PublishError::PublishSend(
                                group.file.clone(),
                                current_registry.clone().into(),
                                PublishSendError::RedirectLocationInvalidStr(err).into(),
                            )
                        })?;
                    current_registry = DisplaySafeUrl::parse(location).map_err(|err| {
                        PublishError::PublishSend(
                            group.file.clone(),
                            current_registry.clone().into(),
                            PublishSendError::RedirectInvalidLocation(err).into(),
                        )