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:
parent
1e51a7ac44
commit
16366cf9f2
294 changed files with 2037 additions and 2610 deletions
|
@ -1,6 +1,6 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context as _, Result};
|
||||
use collections::HashMap;
|
||||
use futures::{SinkExt, channel::mpsc};
|
||||
use gpui::{App, AsyncApp, ScreenCaptureSource, ScreenCaptureStream, Task};
|
||||
|
@ -160,7 +160,7 @@ impl LocalParticipant {
|
|||
})?
|
||||
.await?
|
||||
.map(LocalTrackPublication)
|
||||
.map_err(|error| anyhow::anyhow!("failed to publish track: {error}"))
|
||||
.context("publishing a track")
|
||||
}
|
||||
|
||||
pub async fn unpublish_track(
|
||||
|
@ -172,7 +172,7 @@ impl LocalParticipant {
|
|||
Tokio::spawn(cx, async move { participant.unpublish_track(&sid).await })?
|
||||
.await?
|
||||
.map(LocalTrackPublication)
|
||||
.map_err(|error| anyhow::anyhow!("failed to unpublish track: {error}"))
|
||||
.context("unpublishing a track")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use anyhow::{Context as _, Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait as _};
|
||||
use futures::channel::mpsc::UnboundedSender;
|
||||
|
@ -365,14 +365,14 @@ fn default_device(input: bool) -> Result<(cpal::Device, cpal::SupportedStreamCon
|
|||
if input {
|
||||
device = cpal::default_host()
|
||||
.default_input_device()
|
||||
.ok_or_else(|| anyhow!("no audio input device available"))?;
|
||||
.context("no audio input device available")?;
|
||||
config = device
|
||||
.default_input_config()
|
||||
.context("failed to get default input config")?;
|
||||
} else {
|
||||
device = cpal::default_host()
|
||||
.default_output_device()
|
||||
.ok_or_else(|| anyhow!("no audio output device available"))?;
|
||||
.context("no audio output device available")?;
|
||||
config = device
|
||||
.default_output_config()
|
||||
.context("failed to get default output config")?;
|
||||
|
@ -493,10 +493,7 @@ fn create_buffer_pool(
|
|||
]);
|
||||
|
||||
pixel_buffer_pool::CVPixelBufferPool::new(None, Some(&buffer_attributes)).map_err(|cv_return| {
|
||||
anyhow!(
|
||||
"failed to create pixel buffer pool: CVReturn({})",
|
||||
cv_return
|
||||
)
|
||||
anyhow::anyhow!("failed to create pixel buffer pool: CVReturn({cv_return})",)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -707,7 +704,7 @@ mod macos {
|
|||
}
|
||||
|
||||
impl super::DeviceChangeListenerApi for CoreAudioDefaultDeviceChangeListener {
|
||||
fn new(input: bool) -> gpui::Result<Self> {
|
||||
fn new(input: bool) -> anyhow::Result<Self> {
|
||||
let (tx, rx) = futures::channel::mpsc::unbounded();
|
||||
|
||||
let callback = Box::new(PropertyListenerCallbackWrapper(Box::new(move || {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{AudioStream, Participant, RemoteTrack, RoomEvent, TrackPublication};
|
||||
|
||||
use crate::mock_client::{participant::*, publication::*, track::*};
|
||||
use anyhow::{Context as _, Result, anyhow};
|
||||
use anyhow::{Context as _, Result};
|
||||
use async_trait::async_trait;
|
||||
use collections::{BTreeMap, HashMap, HashSet, btree_map::Entry as BTreeEntry, hash_map::Entry};
|
||||
use gpui::{App, AsyncApp, BackgroundExecutor};
|
||||
|
@ -69,7 +69,7 @@ impl TestServer {
|
|||
e.insert(server.clone());
|
||||
Ok(server)
|
||||
} else {
|
||||
Err(anyhow!("a server with url {:?} already exists", url))
|
||||
anyhow::bail!("a server with url {url:?} already exists");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ impl TestServer {
|
|||
Ok(SERVERS
|
||||
.lock()
|
||||
.get(url)
|
||||
.ok_or_else(|| anyhow!("no server found for url"))?
|
||||
.context("no server found for url")?
|
||||
.clone())
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ impl TestServer {
|
|||
SERVERS
|
||||
.lock()
|
||||
.remove(&self.url)
|
||||
.ok_or_else(|| anyhow!("server with url {:?} does not exist", self.url))?;
|
||||
.with_context(|| format!("server with url {:?} does not exist", self.url))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,7 @@ impl TestServer {
|
|||
e.insert(Default::default());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("room {:?} already exists", room))
|
||||
anyhow::bail!("{room:?} already exists");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,7 +113,7 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
server_rooms
|
||||
.remove(&room)
|
||||
.ok_or_else(|| anyhow!("room {:?} does not exist", room))?;
|
||||
.with_context(|| format!("room {room:?} does not exist"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -176,11 +176,7 @@ impl TestServer {
|
|||
e.insert(client_room);
|
||||
Ok(identity)
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"{:?} attempted to join room {:?} twice",
|
||||
identity,
|
||||
room_name
|
||||
))
|
||||
anyhow::bail!("{identity:?} attempted to join room {room_name:?} twice");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,13 +189,9 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.client_rooms.remove(&identity).ok_or_else(|| {
|
||||
anyhow!(
|
||||
"{:?} attempted to leave room {:?} before joining it",
|
||||
identity,
|
||||
room_name
|
||||
)
|
||||
.with_context(|| format!("room {room_name:?} does not exist"))?;
|
||||
room.client_rooms.remove(&identity).with_context(|| {
|
||||
format!("{identity:?} attempted to leave room {room_name:?} before joining it")
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -247,14 +239,10 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.client_rooms.remove(&identity).ok_or_else(|| {
|
||||
anyhow!(
|
||||
"participant {:?} did not join room {:?}",
|
||||
identity,
|
||||
room_name
|
||||
)
|
||||
})?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
room.client_rooms
|
||||
.remove(&identity)
|
||||
.with_context(|| format!("participant {identity:?} did not join room {room_name:?}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -269,7 +257,7 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
room.participant_permissions
|
||||
.insert(ParticipantIdentity(identity), permission);
|
||||
Ok(())
|
||||
|
@ -308,7 +296,7 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
|
||||
let can_publish = room
|
||||
.participant_permissions
|
||||
|
@ -317,9 +305,7 @@ impl TestServer {
|
|||
.or(claims.video.can_publish)
|
||||
.unwrap_or(true);
|
||||
|
||||
if !can_publish {
|
||||
return Err(anyhow!("user is not allowed to publish"));
|
||||
}
|
||||
anyhow::ensure!(can_publish, "user is not allowed to publish");
|
||||
|
||||
let sid: TrackSid = format!("TR_{}", nanoid::nanoid!(17)).try_into().unwrap();
|
||||
let server_track = Arc::new(TestServerVideoTrack {
|
||||
|
@ -374,7 +360,7 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
|
||||
let can_publish = room
|
||||
.participant_permissions
|
||||
|
@ -383,9 +369,7 @@ impl TestServer {
|
|||
.or(claims.video.can_publish)
|
||||
.unwrap_or(true);
|
||||
|
||||
if !can_publish {
|
||||
return Err(anyhow!("user is not allowed to publish"));
|
||||
}
|
||||
anyhow::ensure!(can_publish, "user is not allowed to publish");
|
||||
|
||||
let sid: TrackSid = format!("TR_{}", nanoid::nanoid!(17)).try_into().unwrap();
|
||||
let server_track = Arc::new(TestServerAudioTrack {
|
||||
|
@ -443,7 +427,7 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
if let Some(track) = room
|
||||
.audio_tracks
|
||||
.iter_mut()
|
||||
|
@ -513,11 +497,11 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
let client_room = room
|
||||
.client_rooms
|
||||
.get(&identity)
|
||||
.ok_or_else(|| anyhow!("not a participant in room"))?;
|
||||
.context("not a participant in room")?;
|
||||
Ok(room
|
||||
.video_tracks
|
||||
.iter()
|
||||
|
@ -536,11 +520,11 @@ impl TestServer {
|
|||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
.with_context(|| format!("room {room_name} does not exist"))?;
|
||||
let client_room = room
|
||||
.client_rooms
|
||||
.get(&identity)
|
||||
.ok_or_else(|| anyhow!("not a participant in room"))?;
|
||||
.context("not a participant in room")?;
|
||||
Ok(room
|
||||
.audio_tracks
|
||||
.iter()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue