From 0d9cd9c6b8c3071cd24f4bf61db456745944d22b Mon Sep 17 00:00:00 2001 From: objz Date: Tue, 16 Jun 2026 12:24:02 +0200 Subject: [PATCH] fixed duplicate logs --- src/instance/loader/forge.rs | 2 -- src/instance/loader/neoforge.rs | 1 - src/instance/manager.rs | 59 ++++++++++++++++++++++++------ src/net/mod.rs | 64 +++++++++++++++------------------ src/tui/event.rs | 1 - src/tui/input.rs | 8 ++--- 6 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/instance/loader/forge.rs b/src/instance/loader/forge.rs index 9694490..e0ad385 100644 --- a/src/instance/loader/forge.rs +++ b/src/instance/loader/forge.rs @@ -70,7 +70,6 @@ impl ModLoaderInstaller for ForgeInstaller { .await { let _ = tokio::fs::remove_file(&installer_jar).await; - tracing::error!("Legacy Forge installation failed: {}", e); return Err(e); } } else { @@ -85,7 +84,6 @@ impl ModLoaderInstaller for ForgeInstaller { forge_api::run_forge_installer(&installer_jar, instance_dir, &java_path).await { let _ = tokio::fs::remove_file(&installer_jar).await; - tracing::error!("Modern Forge installer failed: {}", e); return Err(e); } diff --git a/src/instance/loader/neoforge.rs b/src/instance/loader/neoforge.rs index c8c563b..6159623 100644 --- a/src/instance/loader/neoforge.rs +++ b/src/instance/loader/neoforge.rs @@ -62,7 +62,6 @@ impl ModLoaderInstaller for NeoForgeInstaller { neoforge_api::run_neoforge_installer(&installer_jar, instance_dir, &java_path).await { let _ = tokio::fs::remove_file(&installer_jar).await; - tracing::error!("NeoForge installer failed: {}", e); return Err(e); } diff --git a/src/instance/manager.rs b/src/instance/manager.rs index 7ec88b7..d7ad994 100644 --- a/src/instance/manager.rs +++ b/src/instance/manager.rs @@ -92,8 +92,11 @@ impl InstanceManager { .await; // clean up on failure so there's no half-baked instance left around - if let Err(e) = &result { - tracing::error!("Instance '{}' creation failed: {}", name, e); + if result.is_err() { + tracing::debug!( + "Cleaning up incomplete instance '{}' after creation failed", + name + ); if let Err(cleanup_error) = std::fs::remove_dir_all(&instance_dir) { tracing::warn!( "Failed to clean up incomplete instance directory {}: {}", @@ -171,14 +174,10 @@ impl InstanceManager { v } None => { - tracing::warn!( + return Err(InstanceError::InvalidName(format!( "Minecraft version '{}' not found in manifest with {} entries", game_version, manifest.versions.len() - ); - return Err(InstanceError::InvalidName(format!( - "Minecraft version '{}' not found in manifest", - game_version ))); } }; @@ -282,7 +281,14 @@ impl InstanceManager { return Err(InstanceError::NotFound(name.to_string())); } tracing::info!("Deleting instance '{}' at {}", name, instance_dir.display()); - std::fs::remove_dir_all(&instance_dir)?; + if let Err(e) = std::fs::remove_dir_all(&instance_dir) { + tracing::error!( + "Failed to delete instance directory {}: {}", + instance_dir.display(), + e + ); + return Err(e.into()); + } if let Err(e) = crate::instance::desktop::remove(name) { tracing::warn!("Failed to remove desktop shortcut for '{}': {}", name, e); } @@ -292,6 +298,7 @@ impl InstanceManager { pub fn rename(&self, old_name: &str, new_name: &str) -> Result<(), InstanceError> { let new_name = new_name.trim(); if new_name.is_empty() { + tracing::warn!("Cannot rename instance '{}': new name is empty", old_name); return Err(InstanceError::InvalidName( "Name cannot be empty".to_string(), )); @@ -320,7 +327,15 @@ impl InstanceManager { return Err(InstanceError::AlreadyExists(new_name.to_string())); } tracing::info!("Renaming instance '{}' to '{}'", old_name, new_name); - std::fs::rename(&old_dir, &new_dir)?; + if let Err(e) = std::fs::rename(&old_dir, &new_dir) { + tracing::error!( + "Failed to rename instance directory {} to {}: {}", + old_dir.display(), + new_dir.display(), + e + ); + return Err(e.into()); + } let config_path = new_dir.join("instance.json"); if let Ok(data) = std::fs::read_to_string(&config_path) @@ -402,8 +417,30 @@ impl InstanceManager { } tracing::debug!("Loading instance '{}' from {}", name, config_path.display()); - let contents = std::fs::read_to_string(&config_path)?; - Ok(serde_json::from_str::(&contents)?) + let contents = match std::fs::read_to_string(&config_path) { + Ok(contents) => contents, + Err(e) => { + tracing::error!( + "Failed to read instance '{}' config {}: {}", + name, + config_path.display(), + e + ); + return Err(e.into()); + } + }; + match serde_json::from_str::(&contents) { + Ok(config) => Ok(config), + Err(e) => { + tracing::error!( + "Failed to parse instance '{}' config {}: {}", + name, + config_path.display(), + e + ); + Err(e.into()) + } + } } pub fn save(&self, instance: &InstanceConfig) -> Result<(), InstanceError> { diff --git a/src/net/mod.rs b/src/net/mod.rs index f8c095f..78c0383 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -66,7 +66,7 @@ impl HttpClient { tracing::trace!("HTTP GET {}", url); let response = self.inner.get(url).send().await?; if !response.status().is_success() { - tracing::warn!( + tracing::debug!( "HTTP GET {} returned non-success status {}", url, response.status() @@ -119,42 +119,47 @@ where F: Fn(reqwest::Response) -> Fut, Fut: std::future::Future>, { - let mut last_error = None; for attempt in 0..=MAX_RETRIES { - if attempt > 0 { - let delay = RETRY_BASE_DELAY_MS * 2u64.pow(attempt - 1); - tracing::warn!( - "retrying request after {}ms (attempt {}/{}): {}", - delay, - attempt + 1, - MAX_RETRIES + 1, - url - ); - tokio::time::sleep(std::time::Duration::from_millis(delay)).await; - } - match client.get(url).await { Ok(resp) => match decode(resp).await { Ok(value) => return Ok(value), Err(e) if is_retryable(&e) => { - tracing::warn!("Retryable decode/request error for {}: {}", url, e); - last_error = Some(e); + if attempt == MAX_RETRIES { + return Err(e); + } + sleep_before_retry("request", url, attempt, &e).await; } Err(e) => return Err(e), }, Err(e) if is_retryable(&e) => { - tracing::warn!("Retryable request error for {}: {}", url, e); - last_error = Some(e); + if attempt == MAX_RETRIES { + return Err(e); + } + sleep_before_retry("request", url, attempt, &e).await; } Err(e) => return Err(e), } } - Err(last_error.unwrap()) + unreachable!("retry loop returns on success or final error") } const MAX_RETRIES: u32 = 3; const RETRY_BASE_DELAY_MS: u64 = 500; +async fn sleep_before_retry(kind: &str, url: &str, attempt: u32, err: &NetError) { + let delay = RETRY_BASE_DELAY_MS * 2u64.pow(attempt); + tracing::warn!( + "{} failed, retrying after {}ms (attempt {}/{}): {}: {}", + kind, + delay, + attempt + 2, + MAX_RETRIES + 1, + url, + err + ); + tokio::time::sleep(std::time::Duration::from_millis(delay)).await; +} + // streams a file to disk in chunks, calling progress_cb(downloaded, total) along the way. // total will be 0 if the server doesn't send content-length, so callers // should handle that gracefully. retries transient failures with exponential backoff. @@ -164,36 +169,25 @@ pub async fn download_file( dest: &Path, progress_cb: impl Fn(u64, u64), ) -> Result<(), NetError> { - let mut last_error = None; tracing::debug!("Downloading {} to {}", url, dest.display()); for attempt in 0..=MAX_RETRIES { - if attempt > 0 { - let delay = RETRY_BASE_DELAY_MS * 2u64.pow(attempt - 1); - tracing::warn!( - "retrying download after {}ms (attempt {}/{}): {}", - delay, - attempt + 1, - MAX_RETRIES + 1, - url - ); - tokio::time::sleep(std::time::Duration::from_millis(delay)).await; - } - match download_file_once(client, url, dest, &progress_cb).await { Ok(()) => { tracing::debug!("Downloaded {} to {}", url, dest.display()); return Ok(()); } Err(e) if is_retryable(&e) => { - tracing::warn!("Retryable download error for {}: {}", url, e); - last_error = Some(e); + if attempt == MAX_RETRIES { + return Err(e); + } + sleep_before_retry("download", url, attempt, &e).await; } Err(e) => return Err(e), } } - Err(last_error.unwrap()) + unreachable!("retry loop returns on success or final error") } // single attempt at downloading a file to disk diff --git a/src/tui/event.rs b/src/tui/event.rs index 199359b..465f36f 100644 --- a/src/tui/event.rs +++ b/src/tui/event.rs @@ -254,7 +254,6 @@ impl App { let instance = match self.instance_manager.load_one(&instance.name) { Ok(config) => config, Err(e) => { - tracing::error!("Failed to load instance '{}': {}", instance.name, e); error_buffer::push_error(error_buffer::ErrorEvent { id: 0, level: tracing::Level::ERROR, diff --git a/src/tui/input.rs b/src/tui/input.rs index de0c0cf..61b9f3c 100644 --- a/src/tui/input.rs +++ b/src/tui/input.rs @@ -76,9 +76,7 @@ impl App { Ok(_) => { self.instances_state.remove_instance(&name); } - Err(e) => { - tracing::error!("Failed to delete instance '{}': {}", name, e); - } + Err(_) => {} } } confirm_popup::clear_pending(); @@ -203,9 +201,7 @@ impl App { inst.name = new_name.trim().to_owned(); } } - Err(e) => { - tracing::error!("Rename failed: {}", e); - } + Err(_) => {} } } }