Beautify error logging

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2021-08-24 08:15:24 -06:00
parent 54b4a4bf6a
commit d34f374fe7
3 changed files with 87 additions and 30 deletions

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
rpc::{self, Client}, rpc::{self, Client},
util::log_async_errors, util::TryFutureExt,
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use gpui::{ use gpui::{
@ -76,7 +76,7 @@ impl ChannelList {
pub fn new(rpc: Arc<rpc::Client>, cx: &mut ModelContext<Self>) -> Self { pub fn new(rpc: Arc<rpc::Client>, cx: &mut ModelContext<Self>) -> Self {
cx.spawn(|this, mut cx| { cx.spawn(|this, mut cx| {
let rpc = rpc.clone(); let rpc = rpc.clone();
log_async_errors(async move { async move {
let response = rpc let response = rpc
.request(proto::GetChannels {}) .request(proto::GetChannels {})
.await .await
@ -87,7 +87,8 @@ impl ChannelList {
cx.notify(); cx.notify();
}); });
Ok(()) Ok(())
}) }
.log_err()
}) })
.detach(); .detach();
Self { Self {
@ -185,7 +186,7 @@ impl Channel {
}); });
let rpc = self.rpc.clone(); let rpc = self.rpc.clone();
cx.spawn(|this, mut cx| { cx.spawn(|this, mut cx| {
log_async_errors(async move { async move {
let request = rpc.request(proto::SendChannelMessage { channel_id, body }); let request = rpc.request(proto::SendChannelMessage { channel_id, body });
let response = request.await?; let response = request.await?;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
@ -205,7 +206,7 @@ impl Channel {
} }
}); });
Ok(()) Ok(())
}) }.log_err()
}) })
.detach(); .detach();
cx.notify(); cx.notify();

View File

@ -1,7 +1,11 @@
use futures::Future; use futures::{Future};
pub use gpui::sum_tree::Bias; pub use gpui::sum_tree::Bias;
use rand::prelude::*; use rand::prelude::*;
use std::cmp::Ordering; use std::{
cmp::Ordering,
pin::Pin,
task::{Context, Poll},
};
pub fn post_inc(value: &mut usize) -> usize { pub fn post_inc(value: &mut usize) -> usize {
let prev = *value; let prev = *value;
@ -60,12 +64,58 @@ impl<T: Rng> Iterator for RandomCharIter<T> {
} }
} }
pub async fn log_async_errors<F>(f: F) pub trait ResultExt {
type Ok;
fn log_err(self) -> Option<Self::Ok>;
}
impl<T> ResultExt for anyhow::Result<T> {
type Ok = T;
fn log_err(self) -> Option<T> {
match self {
Ok(value) => Some(value),
Err(error) => {
log::error!("{:?}", error);
None
}
}
}
}
pub trait TryFutureExt {
fn log_err(self) -> LogErrorFuture<Self>
where where
F: Future<Output = anyhow::Result<()>>, Self: Sized;
}
impl<F, T> TryFutureExt for F
where
F: Future<Output = anyhow::Result<T>>,
{ {
if let Err(error) = f.await { fn log_err(self) -> LogErrorFuture<Self>
log::error!("{}", error) where
Self: Sized,
{
LogErrorFuture(self)
}
}
pub struct LogErrorFuture<F>(F);
impl<F, T> Future for LogErrorFuture<F>
where
F: Future<Output = anyhow::Result<T>>,
{
type Output = Option<T>;
fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
match inner.poll(cx) {
Poll::Ready(output) => Poll::Ready(output.log_err()),
Poll::Pending => Poll::Pending,
}
} }
} }

View File

@ -9,7 +9,7 @@ use crate::{
language::LanguageRegistry, language::LanguageRegistry,
rpc::{self, proto}, rpc::{self, proto},
time::{self, ReplicaId}, time::{self, ReplicaId},
util::{log_async_errors, Bias}, util::{Bias, TryFutureExt},
}; };
use ::ignore::gitignore::Gitignore; use ::ignore::gitignore::Gitignore;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -330,10 +330,13 @@ impl Worktree {
.open_remote_buffer(envelope, cx); .open_remote_buffer(envelope, cx);
cx.background() cx.background()
.spawn(log_async_errors(async move { .spawn(
async move {
rpc.respond(receipt, response.await?).await?; rpc.respond(receipt, response.await?).await?;
Ok(()) Ok(())
})) }
.log_err(),
)
.detach(); .detach();
Ok(()) Ok(())
@ -465,7 +468,8 @@ impl Worktree {
}); });
cx.background() cx.background()
.spawn(log_async_errors(async move { .spawn(
async move {
let (version, mtime) = save.await?; let (version, mtime) = save.await?;
rpc.respond( rpc.respond(
@ -480,7 +484,9 @@ impl Worktree {
.await?; .await?;
Ok(()) Ok(())
})) }
.log_err(),
)
.detach(); .detach();
Ok(()) Ok(())