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:
@@ -4,12 +4,8 @@ use futures::{SinkExt as _, StreamExt as _};
|
||||
pub struct Connection {
|
||||
pub(crate) tx:
|
||||
Box<dyn 'static + Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
||||
pub(crate) rx: Box<
|
||||
dyn 'static
|
||||
+ Send
|
||||
+ Unpin
|
||||
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
||||
>,
|
||||
pub(crate) rx:
|
||||
Box<dyn 'static + Send + Unpin + futures::Stream<Item = anyhow::Result<WebSocketMessage>>>,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
@@ -19,7 +15,7 @@ impl Connection {
|
||||
+ Send
|
||||
+ Unpin
|
||||
+ futures::Sink<WebSocketMessage, Error = anyhow::Error>
|
||||
+ futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>,
|
||||
+ futures::Stream<Item = anyhow::Result<WebSocketMessage>>,
|
||||
{
|
||||
let (tx, rx) = stream.split();
|
||||
Self {
|
||||
@@ -28,7 +24,7 @@ impl Connection {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(&mut self, message: WebSocketMessage) -> Result<(), anyhow::Error> {
|
||||
pub async fn send(&mut self, message: WebSocketMessage) -> anyhow::Result<()> {
|
||||
self.tx.send(message).await
|
||||
}
|
||||
|
||||
@@ -56,7 +52,7 @@ impl Connection {
|
||||
executor: gpui::BackgroundExecutor,
|
||||
) -> (
|
||||
Box<dyn Send + Unpin + futures::Sink<WebSocketMessage, Error = anyhow::Error>>,
|
||||
Box<dyn Send + Unpin + futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>>>,
|
||||
Box<dyn Send + Unpin + futures::Stream<Item = anyhow::Result<WebSocketMessage>>>,
|
||||
) {
|
||||
use anyhow::anyhow;
|
||||
use futures::channel::mpsc;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
pub use ::proto::*;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use async_tungstenite::tungstenite::Message as WebSocketMessage;
|
||||
use futures::{SinkExt as _, StreamExt as _};
|
||||
use proto::Message as _;
|
||||
@@ -40,7 +39,7 @@ impl<S> MessageStream<S>
|
||||
where
|
||||
S: futures::Sink<WebSocketMessage, Error = anyhow::Error> + Unpin,
|
||||
{
|
||||
pub async fn write(&mut self, message: Message) -> Result<(), anyhow::Error> {
|
||||
pub async fn write(&mut self, message: Message) -> anyhow::Result<()> {
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
const COMPRESSION_LEVEL: i32 = -7;
|
||||
|
||||
@@ -81,9 +80,9 @@ where
|
||||
|
||||
impl<S> MessageStream<S>
|
||||
where
|
||||
S: futures::Stream<Item = Result<WebSocketMessage, anyhow::Error>> + Unpin,
|
||||
S: futures::Stream<Item = anyhow::Result<WebSocketMessage>> + Unpin,
|
||||
{
|
||||
pub async fn read(&mut self) -> Result<(Message, Instant), anyhow::Error> {
|
||||
pub async fn read(&mut self) -> anyhow::Result<(Message, Instant)> {
|
||||
while let Some(bytes) = self.stream.next().await {
|
||||
let received_at = Instant::now();
|
||||
match bytes? {
|
||||
@@ -102,7 +101,7 @@ where
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Err(anyhow!("connection closed"))
|
||||
anyhow::bail!("connection closed");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +112,7 @@ mod tests {
|
||||
#[gpui::test]
|
||||
async fn test_buffer_size() {
|
||||
let (tx, rx) = futures::channel::mpsc::unbounded();
|
||||
let mut sink = MessageStream::new(tx.sink_map_err(|_| anyhow!("")));
|
||||
let mut sink = MessageStream::new(tx.sink_map_err(|_| anyhow::anyhow!("")));
|
||||
sink.write(Message::Envelope(Envelope {
|
||||
payload: Some(envelope::Payload::UpdateWorktree(UpdateWorktree {
|
||||
root_name: "abcdefg".repeat(10),
|
||||
|
||||
+12
-13
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use anyhow::anyhow;
|
||||
use anyhow::Context;
|
||||
use collections::HashMap;
|
||||
use futures::{
|
||||
Future, FutureExt as _,
|
||||
@@ -190,7 +190,7 @@ impl AnyProtoClient {
|
||||
let response = self.0.request(envelope, T::NAME);
|
||||
async move {
|
||||
T::Response::from_envelope(response.await?)
|
||||
.ok_or_else(|| anyhow!("received response of the wrong type"))
|
||||
.context("received response of the wrong type")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user