persistent_scheduler/core/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use tokio::task::JoinError;

#[derive(Debug, thiserror::Error)]
/// Error type for task processing failures
pub enum SchedulerError {
    /// Error occurred during task execution
    #[error("{0}")]
    Execution(String),

    /// Failed to parse the provided arguments as JSON
    #[error("Invalid JSON format in task arguments")]
    InvalidJson,

    /// Task type not recognized for the client
    #[error("Unrecognized task type for the client")]
    UnrecognizedTask,

    #[error("{0}")]
    Unexpected(String),

    #[error("Can not init task store")]
    StoreInit,
}

impl SchedulerError {
    pub fn panic() -> Self {
        SchedulerError::Execution("task panicked".to_owned())
    }

    pub fn unexpect(e: JoinError) -> Self {
        SchedulerError::Unexpected(format!("task failed unexpectedly: {:?}", e))
    }

    pub fn unrecognized() -> Self {
        SchedulerError::UnrecognizedTask
    }
    pub fn description(&self) -> &'static str {
        match self {
            SchedulerError::Execution(_) => {
                "Execution: Error occurred during task execution or runtime."
            }
            SchedulerError::InvalidJson => {
                "InvalidJson: Error occurred while initializing the task, invalid JSON format."
            }
            SchedulerError::UnrecognizedTask => {
                "UnrecognizedTask: System error, could not find cached job code."
            }
            SchedulerError::Unexpected(_) => "Unexpected: An unexpected error occurred.",

            SchedulerError::StoreInit => {
                "StoreInit: Failed to initialize the task store, possibly due to configuration or connection issues."
            }
        }
    }
}

pub struct BoxError(Box<dyn std::error::Error + Send + Sync>);

impl BoxError {
    pub fn new<E>(error: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        BoxError(Box::new(error))
    }
}

impl std::fmt::Debug for BoxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl std::fmt::Display for BoxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<E> From<E> for BoxError
where
    E: std::error::Error + Send + Sync + 'static,
{
    fn from(error: E) -> Self {
        BoxError::new(error)
    }
}

impl std::ops::Deref for BoxError {
    type Target = dyn std::error::Error + Send + Sync;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}