Skip to main content

aster_forge_tasks/
error.rs

1//! Error type used by product-neutral task helpers.
2
3/// Result type returned by shared task helpers.
4pub type Result<T> = std::result::Result<T, TaskCoreError>;
5
6/// Error returned by shared task helpers before product-level mapping.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum TaskCoreError {
9    /// A stored task value could not be decoded or serialized.
10    #[error("{0}")]
11    Codec(String),
12    /// A task step or registry lookup failed semantic validation.
13    #[error("{0}")]
14    InvalidValue(String),
15    /// Runtime filesystem work failed.
16    #[error("{0}")]
17    Io(String),
18    /// The current worker lost its processing lease to another worker.
19    #[error("background task lease lost for task #{task_id} with token {processing_token}")]
20    LeaseLost {
21        /// Persisted task identifier.
22        task_id: i64,
23        /// Processing token owned by the current worker.
24        processing_token: i64,
25    },
26    /// The current worker did not renew its processing lease before the safety deadline.
27    #[error(
28        "background task lease renewal timed out for task #{task_id} with token {processing_token}"
29    )]
30    LeaseRenewalTimedOut {
31        /// Persisted task identifier.
32        task_id: i64,
33        /// Processing token owned by the current worker.
34        processing_token: i64,
35    },
36    /// The current worker observed shutdown and should release its processing lease.
37    #[error(
38        "background task worker shutdown requested for task #{task_id} with token {processing_token}"
39    )]
40    WorkerShutdownRequested {
41        /// Persisted task identifier.
42        task_id: i64,
43        /// Processing token owned by the current worker.
44        processing_token: i64,
45    },
46}
47
48impl TaskCoreError {
49    /// Creates a codec error.
50    pub fn codec(message: impl Into<String>) -> Self {
51        Self::Codec(message.into())
52    }
53
54    /// Creates an invalid-value error.
55    pub fn invalid_value(message: impl Into<String>) -> Self {
56        Self::InvalidValue(message.into())
57    }
58
59    /// Creates an I/O error.
60    pub fn io(message: impl Into<String>) -> Self {
61        Self::Io(message.into())
62    }
63
64    /// Returns whether this error means the current worker lost its lease.
65    pub const fn is_task_lease_lost(&self) -> bool {
66        matches!(self, Self::LeaseLost { .. })
67    }
68
69    /// Returns whether this error means the current worker's lease renewal timed out.
70    pub const fn is_task_lease_renewal_timed_out(&self) -> bool {
71        matches!(self, Self::LeaseRenewalTimedOut { .. })
72    }
73
74    /// Returns whether this error means the current worker should stop for shutdown.
75    pub const fn is_task_worker_shutdown_requested(&self) -> bool {
76        matches!(self, Self::WorkerShutdownRequested { .. })
77    }
78}