use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
#[error("task '{name}' failed: {source}")]
TaskFailed {
name: &'static str,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("execution cancelled")]
Cancelled,
#[error("execution timed out")]
Timeout,
#[error("join error: {0}")]
Join(#[from] tokio::task::JoinError),
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn task_failed_message_includes_name() {
let err = Error::TaskFailed {
name: "fetch_user",
source: "boom".into(),
};
assert!(err.to_string().contains("fetch_user"));
assert!(err.to_string().contains("boom"));
}
#[test]
fn cancelled_renders() {
assert_eq!(Error::Cancelled.to_string(), "execution cancelled");
}
#[test]
fn timeout_renders() {
assert_eq!(Error::Timeout.to_string(), "execution timed out");
}
}