Skip to main content

altair_concurrent/
error.rs

1//! Errors produced by parallel task execution.
2
3use thiserror::Error;
4
5/// Errors returned by [`crate::execute_concurrently`].
6#[derive(Debug, Error)]
7#[non_exhaustive]
8pub enum Error {
9    /// A task returned an error; remaining tasks were cancelled.
10    #[error("task '{name}' failed: {source}")]
11    TaskFailed {
12        /// The static name of the failing task.
13        name: &'static str,
14        /// The underlying error.
15        #[source]
16        source: Box<dyn std::error::Error + Send + Sync>,
17    },
18
19    /// The cancellation token fired before all tasks completed.
20    #[error("execution cancelled")]
21    Cancelled,
22
23    /// The configured timeout elapsed before all tasks completed.
24    #[error("execution timed out")]
25    Timeout,
26
27    /// A task panicked or was cancelled by the runtime.
28    #[error("join error: {0}")]
29    Join(#[from] tokio::task::JoinError),
30}
31
32/// Convenience result alias.
33pub type Result<T> = std::result::Result<T, Error>;
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn task_failed_message_includes_name() {
41        let err = Error::TaskFailed {
42            name: "fetch_user",
43            source: "boom".into(),
44        };
45        assert!(err.to_string().contains("fetch_user"));
46        assert!(err.to_string().contains("boom"));
47    }
48
49    #[test]
50    fn cancelled_renders() {
51        assert_eq!(Error::Cancelled.to_string(), "execution cancelled");
52    }
53
54    #[test]
55    fn timeout_renders() {
56        assert_eq!(Error::Timeout.to_string(), "execution timed out");
57    }
58}