Remove annoying error message when task is cancelled

This commit is contained in:
Igor Katson 2024-06-21 15:12:51 +01:00
parent ace4bed0c6
commit 36359150a7
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -1,6 +1,15 @@
use anyhow::bail; use anyhow::bail;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{error, trace, Instrument}; use tracing::{debug, error, trace, Instrument};
#[derive(Debug)]
struct CancelledError {}
impl std::error::Error for CancelledError {}
impl std::fmt::Display for CancelledError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("cancelled")
}
}
/// Spawns a future with tracing instrumentation. /// Spawns a future with tracing instrumentation.
pub fn spawn( pub fn spawn(
@ -23,7 +32,12 @@ pub fn spawn(
trace!("finished"); trace!("finished");
} }
Err(e) => { Err(e) => {
error!("finished with error: {:#}", e) if e.is::<CancelledError>() {
debug!("task cancelled")
} else {
error!("finished with error: {:#}", e)
}
} }
} }
return; return;
@ -43,7 +57,7 @@ pub fn spawn_with_cancel(
spawn(span, async move { spawn(span, async move {
tokio::select! { tokio::select! {
_ = cancellation_token.cancelled() => { _ = cancellation_token.cancelled() => {
bail!("cancelled"); bail!(CancelledError{})
}, },
r = fut => r r = fut => r
} }