From 2ab35c6b7d594288cdfee6695d1afa8f7ce91444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Houl=C3=A9?= <13155277+tomhoule@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:20:52 +0200 Subject: [PATCH] Consistently use `context()` to preserve sources for anyhow errors (#59112) When you use `anyhow::anyhow!("{error}")` to convert a preexisting error to an `anyhow::Error`, the error source can be lost (depending on the `Display` impl of the error). Anyhow errors can display the whole source chain when printed. This commit makes us consistently use `context()` instead to preserve the underlying error's source. Release Notes: - N/A --- crates/agent/src/db.rs | 4 ++-- crates/keymap_editor/src/keymap_editor.rs | 2 +- crates/remote/src/transport/ssh.rs | 4 +++- crates/remote/src/transport/wsl.rs | 8 +++++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/agent/src/db.rs b/crates/agent/src/db.rs index 7d69e03c325..a09aa9c2c13 100644 --- a/crates/agent/src/db.rs +++ b/crates/agent/src/db.rs @@ -2,7 +2,7 @@ use crate::{AgentMessage, AgentMessageContent, UserMessage, UserMessageContent}; use acp_thread::ClientUserMessageId; use agent_client_protocol::schema::v1 as acp; use agent_settings::AgentProfileId; -use anyhow::{Result, anyhow}; +use anyhow::Result; use chrono::{DateTime, Utc}; use collections::{HashMap, IndexMap}; use futures::{FutureExt, future::Shared}; @@ -444,7 +444,7 @@ impl ThreadsDatabase { data BLOB NOT NULL ) "})?() - .map_err(|e| anyhow!("Failed to create threads table: {}", e))?; + .map_err(|e| e.context("Failed to create threads table"))?; if let Ok(mut s) = connection.exec(indoc! {" ALTER TABLE threads ADD COLUMN parent_id TEXT diff --git a/crates/keymap_editor/src/keymap_editor.rs b/crates/keymap_editor/src/keymap_editor.rs index aecaea7fcba..db405135994 100644 --- a/crates/keymap_editor/src/keymap_editor.rs +++ b/crates/keymap_editor/src/keymap_editor.rs @@ -3668,7 +3668,7 @@ async fn save_keybinding_update( keyboard_mapper, deprecated_aliases, ) - .map_err(|err| anyhow::anyhow!("Could not save updated keybinding: {}", err))?; + .map_err(|err| err.context("Could not save updated keybinding"))?; fs.write( paths::keymap_file().as_path(), updated_keymap_contents.as_bytes(), diff --git a/crates/remote/src/transport/ssh.rs b/crates/remote/src/transport/ssh.rs index 25946c0b7c0..f897bdd1d96 100644 --- a/crates/remote/src/transport/ssh.rs +++ b/crates/remote/src/transport/ssh.rs @@ -496,7 +496,9 @@ impl RemoteConnection for SshRemoteConnection { { Ok(process) => process, Err(error) => { - return Task::ready(Err(anyhow!("failed to spawn remote server: {}", error))); + return Task::ready(Err( + anyhow::Error::new(error).context("failed to spawn remote server") + )); } }; diff --git a/crates/remote/src/transport/wsl.rs b/crates/remote/src/transport/wsl.rs index 21ebdcbd049..9b8e7763944 100644 --- a/crates/remote/src/transport/wsl.rs +++ b/crates/remote/src/transport/wsl.rs @@ -210,7 +210,7 @@ impl WslRemoteConnection { let mkdir = self.shell_kind.prepend_command_prefix("mkdir"); self.run_wsl_command(&mkdir, &["-p", &parent]) .await - .map_err(|e| anyhow!("Failed to create directory: {}", e))?; + .map_err(|e| e.context("Failed to create directory"))?; } let binary_exists_on_server = self @@ -346,7 +346,7 @@ impl WslRemoteConnection { self.run_wsl_command("sh", &["-c", &script]) .await - .map_err(|e| anyhow!("Failed to extract server binary: {}", e))?; + .map_err(|e| e.context("Failed to extract server binary"))?; Ok(()) } } @@ -395,7 +395,9 @@ impl RemoteConnection for WslRemoteConnection { { Ok(process) => process, Err(error) => { - return Task::ready(Err(anyhow!("failed to spawn remote server: {}", error))); + return Task::ready(Err( + anyhow::Error::new(error).context("failed to spawn remote server") + )); } };