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
This commit is contained in:
Tom Houlé 2026-07-08 14:20:52 +02:00 committed by GitHub
parent bc3c9422f4
commit 2ab35c6b7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 11 additions and 7 deletions

View file

@ -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

View file

@ -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(),

View file

@ -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")
));
}
};

View file

@ -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")
));
}
};