diff --git a/crates/ide-http/src/client.rs b/crates/ide-http/src/client.rs index 55ed999700..3545ea5580 100644 --- a/crates/ide-http/src/client.rs +++ b/crates/ide-http/src/client.rs @@ -174,9 +174,9 @@ pub async fn check_connection(proxy: &ProxySettings, url: &str, cancel: &Cancell Ok(format!("{} {}", status.as_u16(), status.canonical_reason().unwrap_or("")).trim_end().to_owned()) } -/// reqwest errors with their causes ("error sending request: … connection refused"). -pub fn describe(e: &reqwest::Error) -> String { - use std::error::Error; +/// An error with its causes ("error sending request: … connection refused"), so +/// messages surfaced to the user keep the step that actually failed. +pub fn describe(e: &(dyn std::error::Error + 'static)) -> String { let mut msg = e.to_string(); let mut source = e.source(); while let Some(s) = source { @@ -194,6 +194,51 @@ pub fn describe(e: &reqwest::Error) -> String { mod tests { use super::*; + /// The updater wraps reqwest in its own error type (`tauri-plugin-updater`'s + /// `#[error(transparent)]` variant), so `describe` must follow `source` through + /// any error, not just `reqwest::Error`. + #[derive(Debug)] + struct ConnectRefused; + impl std::fmt::Display for ConnectRefused { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "tcp connect error: connection refused 127.0.0.1:55134") + } + } + impl std::error::Error for ConnectRefused {} + + #[derive(Debug)] + struct WrappedRequest { + source: ConnectRefused, + } + impl std::fmt::Display for WrappedRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "error sending request for url (https://cnb.cool/l0ng-ai/openidea/-/releases/latest/download/latest.json)" + ) + } + } + impl std::error::Error for WrappedRequest { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(&self.source) + } + } + + #[test] + fn describe_keeps_the_causes_behind_a_wrapped_error() { + let error = WrappedRequest { source: ConnectRefused }; + assert_eq!( + describe(&error), + "error sending request for url (https://cnb.cool/l0ng-ai/openidea/-/releases/latest/download/latest.json): \ + tcp connect error: connection refused 127.0.0.1:55134" + ); + } + + #[test] + fn describe_of_an_error_without_causes_is_unchanged() { + assert_eq!(describe(&ConnectRefused), "tcp connect error: connection refused 127.0.0.1:55134"); + } + #[test] fn auto_mode_rebuilds_the_shared_client_when_the_system_proxy_changes() { static SYSTEM: Mutex = Mutex::new(String::new()); diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index ecbaf3177f..0a300d0257 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -755,7 +755,7 @@ pub(crate) async fn check_update(app: &AppHandle, source: UpdateSource) -> IdeRe let update = tokio::time::timeout(std::time::Duration::from_secs(60), updater.check()) .await .map_err(|e| IdeError::unavailable(e.to_string()))? - .map_err(|e| IdeError::unavailable(e.to_string()))?; + .map_err(|e| IdeError::unavailable(ide_http::client::describe(&e)))?; Ok(UpdateCheck { enabled: true, update: update.map(|u| UpdateInfo { diff --git a/src-tauri/src/updates.rs b/src-tauri/src/updates.rs index 16c18f977f..4cba0b7029 100644 --- a/src-tauri/src/updates.rs +++ b/src-tauri/src/updates.rs @@ -195,10 +195,11 @@ pub(crate) async fn download_update( mut progress: impl FnMut(u64, Option), ) -> IdeResult { let updater = updater(app, source)?.ok_or_else(|| IdeError::unavailable("updates are not configured"))?; - let update = - cancellable(token, async { updater.check().await.map_err(|error| IdeError::unavailable(error.to_string())) }) - .await? - .ok_or_else(|| IdeError::not_found("no update available"))?; + let update = cancellable(token, async { + updater.check().await.map_err(|error| IdeError::unavailable(ide_http::client::describe(&error))) + }) + .await? + .ok_or_else(|| IdeError::not_found("no update available"))?; let directory = app .path() .app_cache_dir() diff --git a/src-tauri/src/updates/download.rs b/src-tauri/src/updates/download.rs index eff0369bca..732aa26102 100644 --- a/src-tauri/src/updates/download.rs +++ b/src-tauri/src/updates/download.rs @@ -47,7 +47,8 @@ pub(crate) async fn fetch( if offset > 0 { request = request.header(RANGE, format!("bytes={offset}-")); } - let response = request.send().await.map_err(|error| IdeError::unavailable(error.to_string()))?; + let response = + request.send().await.map_err(|error| IdeError::unavailable(ide_http::client::describe(&error)))?; let status = response.status(); if status == StatusCode::RANGE_NOT_SATISFIABLE && offset > 0 { @@ -171,7 +172,7 @@ async fn append_body( progress(downloaded, total); } Ok(None) => break Ok(()), - Err(error) => break Err(IdeError::unavailable(error.to_string())), + Err(error) => break Err(IdeError::unavailable(ide_http::client::describe(&error))), } }; // tokio writes in the background: flush even when the body broke off, so the bytes