Skip to main content

async_rt/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4#[non_exhaustive]
5pub enum JoinError {
6    /// The task was cancelled without an explicit abort request through its
7    /// [`JoinHandle`](crate::JoinHandle), such as during runtime shutdown.
8    #[error("The task was cancelled")]
9    Cancelled,
10    /// The task was cancelled after [`JoinHandle::abort`](crate::JoinHandle::abort)
11    /// was requested.
12    #[error("The task was aborted")]
13    Aborted,
14    /// The task panicked.
15    #[error("The task panicked")]
16    Panicked,
17    /// The task that was polled was empty or contained no pending future.
18    #[error("The task was empty")]
19    Empty,
20    /// Unknown error.
21    #[error("An unknown error occurred")]
22    Unknown,
23}
24
25#[cfg(all(feature = "tokio", not(target_arch = "wasm32")))]
26impl From<tokio::task::JoinError> for JoinError {
27    fn from(err: tokio::task::JoinError) -> Self {
28        if err.is_cancelled() {
29            return JoinError::Cancelled;
30        }
31
32        if err.is_panic() {
33            return JoinError::Panicked;
34        }
35
36        JoinError::Unknown
37    }
38}
39
40#[cfg(all(feature = "compio", not(target_arch = "wasm32")))]
41impl From<compio::runtime::JoinError> for JoinError {
42    fn from(err: compio::runtime::JoinError) -> Self {
43        match err {
44            compio::runtime::JoinError::Cancelled => JoinError::Cancelled,
45            compio::runtime::JoinError::Panicked(_) => JoinError::Panicked,
46        }
47    }
48}
49
50/// Error indicating a task did not complete before its timeout elapsed.
51///
52/// Returned as the inner error of a timeout task's output, for example from
53/// [`ExecutorTimeout::spawn_timeout`](crate::ExecutorTimeout::spawn_timeout). A timeout does not
54/// produce [`JoinError::Aborted`] or [`JoinError::Cancelled`] as those come from aborting or dropping
55/// the task's handle.
56#[derive(Debug, Error)]
57#[error("The task timed out")]
58pub struct TimeoutError;