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

@ -197,7 +197,7 @@ impl Peer {
}
_ = create_timer(WRITE_TIMEOUT).fuse() => {
tracing::trace!(%connection_id, "outgoing rpc message: writing timed out");
Err(anyhow!("timed out writing message"))?;
anyhow::bail!("timed out writing message");
}
}
}
@ -217,7 +217,7 @@ impl Peer {
}
_ = create_timer(WRITE_TIMEOUT).fuse() => {
tracing::trace!(%connection_id, "keepalive interval: pinging timed out");
Err(anyhow!("timed out sending keepalive"))?;
anyhow::bail!("timed out sending keepalive");
}
}
}
@ -240,7 +240,7 @@ impl Peer {
},
_ = create_timer(WRITE_TIMEOUT).fuse() => {
tracing::trace!(%connection_id, "incoming rpc message: processing timed out");
Err(anyhow!("timed out processing incoming message"))?
anyhow::bail!("timed out processing incoming message");
}
}
}
@ -248,7 +248,7 @@ impl Peer {
},
_ = receive_timeout => {
tracing::trace!(%connection_id, "receive timeout: delay between messages too long");
Err(anyhow!("delay between messages too long"))?
anyhow::bail!("delay between messages too long");
}
}
}
@ -441,7 +441,7 @@ impl Peer {
sender_id: receiver_id.into(),
original_sender_id: response.original_sender_id,
payload: T::Response::from_envelope(response)
.ok_or_else(|| anyhow!("received response of the wrong type"))?,
.context("received response of the wrong type")?,
received_at,
})
}
@ -465,18 +465,17 @@ impl Peer {
.response_channels
.lock()
.as_mut()
.ok_or_else(|| anyhow!("connection was closed"))?
.context("connection was closed")?
.insert(envelope.id, tx);
connection
.outgoing_tx
.unbounded_send(Message::Envelope(envelope))
.map_err(|_| anyhow!("connection was closed"))?;
.context("connection was closed")?;
Ok(())
});
async move {
send?;
let (response, received_at, _barrier) =
rx.await.map_err(|_| anyhow!("connection was closed"))?;
let (response, received_at, _barrier) = rx.await.context("connection was closed")?;
if let Some(proto::envelope::Payload::Error(error)) = &response.payload {
return Err(RpcError::from_proto(error, type_name));
}
@ -496,14 +495,14 @@ impl Peer {
stream_response_channels
.lock()
.as_mut()
.ok_or_else(|| anyhow!("connection was closed"))?
.context("connection was closed")?
.insert(message_id, tx);
connection
.outgoing_tx
.unbounded_send(Message::Envelope(
request.into_envelope(message_id, None, None),
))
.map_err(|_| anyhow!("connection was closed"))?;
.context("connection was closed")?;
Ok((message_id, stream_response_channels))
});
@ -530,7 +529,7 @@ impl Peer {
} else {
Some(
T::Response::from_envelope(response)
.ok_or_else(|| anyhow!("received response of the wrong type")),
.context("received response of the wrong type"),
)
}
}
@ -662,7 +661,7 @@ impl Peer {
let connections = self.connections.read();
let connection = connections
.get(&connection_id)
.ok_or_else(|| anyhow!("no such connection: {}", connection_id))?;
.with_context(|| format!("no such connection: {connection_id}"))?;
Ok(connection.clone())
}
}