wire_framework/service/
error.rs

1use std::fmt;
2
3use crate::{task::TaskId, wiring_layer::WiringError};
4
5/// An error that can occur during the task lifecycle.
6#[derive(Debug, thiserror::Error)]
7pub enum TaskError {
8    #[error("Task {0} failed: {1:#}")]
9    TaskFailed(TaskId, eyre::Report),
10    #[error("Task {0} panicked: {1}")]
11    TaskPanicked(TaskId, String),
12    #[error("Shutdown for task {0} timed out")]
13    TaskShutdownTimedOut(TaskId),
14    #[error("Shutdown hook {0} failed: {1:#}")]
15    ShutdownHookFailed(TaskId, eyre::Report),
16    #[error("Shutdown hook {0} timed out")]
17    ShutdownHookTimedOut(TaskId),
18}
19
20/// Wrapper of a list of errors with a reasonable formatting.
21pub struct TaskErrors(pub Vec<TaskError>);
22
23impl From<Vec<TaskError>> for TaskErrors {
24    fn from(errs: Vec<TaskError>) -> Self {
25        Self(errs)
26    }
27}
28
29impl fmt::Debug for TaskErrors {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        self.0
32            .iter()
33            .map(|err| format!("{err:#}"))
34            .collect::<Vec<_>>()
35            .fmt(f)
36    }
37}
38
39/// An error that can occur during the service lifecycle.
40#[derive(Debug, thiserror::Error)]
41pub enum ServiceError {
42    #[error(
43        "Detected a Tokio Runtime. Service manages its own runtime and does not support nested runtimes"
44    )]
45    RuntimeDetected,
46    #[error("No tasks have been added to the service")]
47    NoTasks,
48    #[error("One or more wiring layers failed to initialize: {0:?}")]
49    Wiring(Vec<(String, WiringError)>),
50    #[error("One or more tasks failed: {0:?}")]
51    Task(TaskErrors),
52}