Remove annoying error message when task is cancelled
This commit is contained in:
parent
ace4bed0c6
commit
36359150a7
1 changed files with 17 additions and 3 deletions
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue