persistent_scheduler/core/
error.rs

1use tokio::task::JoinError;
2
3#[derive(Debug, thiserror::Error)]
4/// Error type for task processing failures
5pub enum SchedulerError {
6    /// Error occurred during task execution
7    #[error("{0}")]
8    Execution(String),
9
10    /// Failed to parse the provided arguments as JSON
11    #[error("Invalid JSON format in task arguments")]
12    InvalidJson,
13
14    /// Task type not recognized for the client
15    #[error("Unrecognized task type for the client")]
16    UnrecognizedTask,
17
18    #[error("{0}")]
19    Unexpected(String),
20
21    #[error("Can not init task store")]
22    StoreInit,
23}
24
25impl SchedulerError {
26    pub fn panic() -> Self {
27        SchedulerError::Execution("task panicked".to_owned())
28    }
29
30    pub fn unexpect(e: JoinError) -> Self {
31        SchedulerError::Unexpected(format!("task failed unexpectedly: {:?}", e))
32    }
33
34    pub fn unrecognized() -> Self {
35        SchedulerError::UnrecognizedTask
36    }
37    pub fn description(&self) -> &'static str {
38        match self {
39            SchedulerError::Execution(_) => {
40                "Execution: Error occurred during task execution or runtime."
41            }
42            SchedulerError::InvalidJson => {
43                "InvalidJson: Error occurred while initializing the task, invalid JSON format."
44            }
45            SchedulerError::UnrecognizedTask => {
46                "UnrecognizedTask: System error, could not find cached job code."
47            }
48            SchedulerError::Unexpected(_) => "Unexpected: An unexpected error occurred.",
49
50            SchedulerError::StoreInit => {
51                "StoreInit: Failed to initialize the task store, possibly due to configuration or connection issues."
52            }
53        }
54    }
55}
56
57pub struct BoxError(Box<dyn std::error::Error + Send + Sync>);
58
59impl BoxError {
60    pub fn new<E>(error: E) -> Self
61    where
62        E: std::error::Error + Send + Sync + 'static,
63    {
64        BoxError(Box::new(error))
65    }
66}
67
68impl std::fmt::Debug for BoxError {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{:?}", self.0)
71    }
72}
73
74impl std::fmt::Display for BoxError {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        write!(f, "{}", self.0)
77    }
78}
79
80impl<E> From<E> for BoxError
81where
82    E: std::error::Error + Send + Sync + 'static,
83{
84    fn from(error: E) -> Self {
85        BoxError::new(error)
86    }
87}
88
89impl std::ops::Deref for BoxError {
90    type Target = dyn std::error::Error + Send + Sync;
91
92    fn deref(&self) -> &Self::Target {
93        &*self.0
94    }
95}