Use anyhow more idiomatically (#31052)

https://github.com/zed-industries/zed/issues/30972 brought up another
case where our context is not enough to track the actual source of the
issue: we get a general top-level error without inner error.

The reason for this was `.ok_or_else(|| anyhow!("failed to read HEAD
SHA"))?; ` on the top level.

The PR finally reworks the way we use anyhow to reduce such issues (or
at least make it simpler to bubble them up later in a fix).
On top of that, uses a few more anyhow methods for better readability.

* `.ok_or_else(|| anyhow!("..."))`, `map_err` and other similar error
conversion/option reporting cases are replaced with `context` and
`with_context` calls
* in addition to that, various `anyhow!("failed to do ...")` are
stripped with `.context("Doing ...")` messages instead to remove the
parasitic `failed to` text
* `anyhow::ensure!` is used instead of `if ... { return Err(...); }`
calls
* `anyhow::bail!` is used instead of `return Err(anyhow!(...));`

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2025-05-21 02:06:07 +03:00 committed by GitHub
parent 1e51a7ac44
commit 16366cf9f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
294 changed files with 2037 additions and 2610 deletions

View file

@ -7,7 +7,7 @@ use crate::{
PrepareRenameResponse, ProjectTransaction, ResolveState,
lsp_store::{LocalLspStore, LspStore},
};
use anyhow::{Context as _, Result, anyhow};
use anyhow::{Context as _, Result};
use async_trait::async_trait;
use client::proto::{self, PeerId};
use clock::Global;
@ -48,9 +48,7 @@ pub fn lsp_formatting_options(settings: &LanguageSettings) -> lsp::FormattingOpt
pub(crate) fn file_path_to_lsp_url(path: &Path) -> Result<lsp::Url> {
match lsp::Url::from_file_path(path) {
Ok(url) => Ok(url),
Err(()) => Err(anyhow!(
"Invalid file path provided to LSP request: {path:?}"
)),
Err(()) => anyhow::bail!("Invalid file path provided to LSP request: {path:?}"),
}
}
@ -293,7 +291,7 @@ impl LspCommand for PrepareRename {
Some(lsp::OneOf::Left(true)) => Ok(LspParamsOrResponse::Response(
PrepareRenameResponse::OnlyUnpreparedRenameSupported,
)),
_ => Err(anyhow!("Rename not supported")),
_ => anyhow::bail!("Rename not supported"),
}
}
@ -359,7 +357,7 @@ impl LspCommand for PrepareRename {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -422,9 +420,9 @@ impl LspCommand for PrepareRename {
) {
Ok(PrepareRenameResponse::Success(start..end))
} else {
Err(anyhow!(
anyhow::bail!(
"Missing start or end position in remote project PrepareRenameResponse"
))
);
}
} else if message.only_unprepared_rename_supported {
Ok(PrepareRenameResponse::OnlyUnpreparedRenameSupported)
@ -508,7 +506,7 @@ impl LspCommand for PerformRename {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -543,9 +541,7 @@ impl LspCommand for PerformRename {
_: Entity<Buffer>,
mut cx: AsyncApp,
) -> Result<ProjectTransaction> {
let message = message
.transaction
.ok_or_else(|| anyhow!("missing transaction"))?;
let message = message.transaction.context("missing transaction")?;
lsp_store
.update(&mut cx, |lsp_store, cx| {
lsp_store.buffer_store().update(cx, |buffer_store, cx| {
@ -622,7 +618,7 @@ impl LspCommand for GetDefinition {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -721,7 +717,7 @@ impl LspCommand for GetDeclaration {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -813,7 +809,7 @@ impl LspCommand for GetImplementation {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -912,7 +908,7 @@ impl LspCommand for GetTypeDefinition {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -963,7 +959,7 @@ fn language_server_for_buffer(
.map(|(adapter, server)| (adapter.clone(), server.clone()))
})
})?
.ok_or_else(|| anyhow!("no language server found for buffer"))
.context("no language server found for buffer")
}
pub async fn location_links_from_proto(
@ -997,11 +993,11 @@ pub fn location_link_from_proto(
let start = origin
.start
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing origin start"))?;
.context("missing origin start")?;
let end = origin
.end
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing origin end"))?;
.context("missing origin end")?;
buffer
.update(cx, |buffer, _| buffer.wait_for_anchors([start, end]))?
.await?;
@ -1013,7 +1009,7 @@ pub fn location_link_from_proto(
None => None,
};
let target = link.target.ok_or_else(|| anyhow!("missing target"))?;
let target = link.target.context("missing target")?;
let buffer_id = BufferId::new(target.buffer_id)?;
let buffer = lsp_store
.update(cx, |lsp_store, cx| {
@ -1023,11 +1019,11 @@ pub fn location_link_from_proto(
let start = target
.start
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target start"))?;
.context("missing target start")?;
let end = target
.end
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target end"))?;
.context("missing target end")?;
buffer
.update(cx, |buffer, _| buffer.wait_for_anchors([start, end]))?
.await?;
@ -1337,7 +1333,7 @@ impl LspCommand for GetReferences {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -1393,11 +1389,11 @@ impl LspCommand for GetReferences {
let start = location
.start
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target start"))?;
.context("missing target start")?;
let end = location
.end
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target end"))?;
.context("missing target end")?;
target_buffer
.update(&mut cx, |buffer, _| buffer.wait_for_anchors([start, end]))?
.await?;
@ -1494,7 +1490,7 @@ impl LspCommand for GetDocumentHighlights {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -1540,11 +1536,11 @@ impl LspCommand for GetDocumentHighlights {
let start = highlight
.start
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target start"))?;
.context("missing target start")?;
let end = highlight
.end
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("missing target end"))?;
.context("missing target end")?;
buffer
.update(&mut cx, |buffer, _| buffer.wait_for_anchors([start, end]))?
.await?;
@ -1723,19 +1719,15 @@ impl LspCommand for GetDocumentSymbols {
let kind =
unsafe { mem::transmute::<i32, lsp::SymbolKind>(serialized_symbol.kind) };
let start = serialized_symbol
.start
.ok_or_else(|| anyhow!("invalid start"))?;
let end = serialized_symbol
.end
.ok_or_else(|| anyhow!("invalid end"))?;
let start = serialized_symbol.start.context("invalid start")?;
let end = serialized_symbol.end.context("invalid end")?;
let selection_start = serialized_symbol
.selection_start
.ok_or_else(|| anyhow!("invalid selection start"))?;
.context("invalid selection start")?;
let selection_end = serialized_symbol
.selection_end
.ok_or_else(|| anyhow!("invalid selection end"))?;
.context("invalid selection end")?;
Ok(DocumentSymbol {
name: serialized_symbol.name,
@ -1993,7 +1985,7 @@ impl LspCommand for GetHover {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -2329,7 +2321,7 @@ impl LspCommand for GetCompletions {
buffer.clip_point_utf16(Unclipped(p.to_point_utf16(buffer)), Bias::Left)
})
})
.ok_or_else(|| anyhow!("invalid position"))??;
.context("invalid position")??;
Ok(Self {
position,
context: CompletionContext {
@ -2597,11 +2589,11 @@ impl LspCommand for GetCodeActions {
let start = message
.start
.and_then(language::proto::deserialize_anchor)
.ok_or_else(|| anyhow!("invalid start"))?;
.context("invalid start")?;
let end = message
.end
.and_then(language::proto::deserialize_anchor)
.ok_or_else(|| anyhow!("invalid end"))?;
.context("invalid end")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -2767,7 +2759,7 @@ impl LspCommand for OnTypeFormatting {
let position = message
.position
.and_then(deserialize_anchor)
.ok_or_else(|| anyhow!("invalid position"))?;
.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
@ -3576,15 +3568,13 @@ impl LspCommand for LinkedEditingRange {
buffer: Entity<Buffer>,
mut cx: AsyncApp,
) -> Result<Self> {
let position = message
.position
.ok_or_else(|| anyhow!("invalid position"))?;
let position = message.position.context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| {
buffer.wait_for_version(deserialize_version(&message.version))
})?
.await?;
let position = deserialize_anchor(position).ok_or_else(|| anyhow!("invalid position"))?;
let position = deserialize_anchor(position).context("invalid position")?;
buffer
.update(&mut cx, |buffer, _| buffer.wait_for_anchors([position]))?
.await?;