Skip to main content

hyprstream_workers/
error.rs

1//! Error types for hyprstream-workers
2
3use thiserror::Error;
4
5/// Main error type for worker operations
6#[derive(Error, Debug)]
7pub enum WorkerError {
8    // ─────────────────────────────────────────────────────────────────────
9    // Sandbox (Pod) Errors
10    // ─────────────────────────────────────────────────────────────────────
11    #[error("Sandbox not found: {0}")]
12    SandboxNotFound(String),
13
14    #[error("Sandbox creation failed: {0}")]
15    SandboxCreationFailed(String),
16
17    #[error("Sandbox already exists: {0}")]
18    SandboxAlreadyExists(String),
19
20    #[error(
21        "Sandbox in invalid state for operation: {sandbox_id} is {state}, expected {expected}"
22    )]
23    SandboxInvalidState {
24        sandbox_id: String,
25        state: String,
26        expected: String,
27    },
28
29    #[error("Sandbox pool exhausted: max {max} sandboxes")]
30    PoolExhausted { max: usize },
31
32    #[error("Sandbox timeout: {operation} took longer than {timeout_secs}s")]
33    SandboxTimeout {
34        operation: String,
35        timeout_secs: u64,
36    },
37
38    // ─────────────────────────────────────────────────────────────────────
39    // Admission control errors (#525 P2 — SandboxPool::acquire decision engine)
40    // ─────────────────────────────────────────────────────────────────────
41    /// Per-Subject or per-group quota exceeded. Fail-fast: unlike capacity
42    /// exhaustion, a quota rejection never queues (retrying immediately
43    /// cannot help until the Subject/group's own active count drops).
44    #[error("Admission denied: {reason}")]
45    AdmissionDenied { reason: String },
46
47    /// The bounded admission wait-queue was already at capacity when this
48    /// request arrived — rejected immediately rather than silently blocking
49    /// past the configured bound.
50    #[error("Admission queue full: {waiting} waiting (bound {bound})")]
51    QueueFull { waiting: usize, bound: usize },
52
53    /// A request queued for capacity did not get admitted within the
54    /// configured wait timeout.
55    #[error("Admission queue wait timed out after {timeout_secs}s")]
56    QueueTimeout { timeout_secs: u64 },
57
58    /// Demand exceeds this node's *configured totals* (e.g. a GPU request on a
59    /// node that declares no GPU capacity, or memory above the declared total)
60    /// — no release can ever satisfy it. Rejected immediately rather than
61    /// queued, so callers can distinguish "never fits here" from "busy right
62    /// now" (`QueueTimeout`).
63    #[error("Admission infeasible: {reason}")]
64    AdmissionInfeasible { reason: String },
65
66    // ─────────────────────────────────────────────────────────────────────
67    // VM Errors
68    // ─────────────────────────────────────────────────────────────────────
69    #[error("VM start failed: {0}")]
70    VmStartFailed(String),
71
72    #[error("VM stop failed: {0}")]
73    VmStopFailed(String),
74
75    #[error("Cloud-init generation failed: {0}")]
76    CloudInitFailed(String),
77
78    #[error("VirtioFS daemon failed: {0}")]
79    VirtiofsFailed(String),
80
81    // ─────────────────────────────────────────────────────────────────────
82    // Container Errors
83    // ─────────────────────────────────────────────────────────────────────
84    #[error("Container not found: {0}")]
85    ContainerNotFound(String),
86
87    #[error("Container creation failed: {0}")]
88    ContainerCreationFailed(String),
89
90    #[error("Container already exists: {0}")]
91    ContainerAlreadyExists(String),
92
93    #[error("Container in invalid state: {container_id} is {state}, expected {expected}")]
94    ContainerInvalidState {
95        container_id: String,
96        state: String,
97        expected: String,
98    },
99
100    #[error("Container exec failed: {0}")]
101    ExecFailed(String),
102
103    // ─────────────────────────────────────────────────────────────────────
104    // Image Errors
105    // ─────────────────────────────────────────────────────────────────────
106    #[error("Image not found: {0}")]
107    ImageNotFound(String),
108
109    #[error("Failed to pull image {image}: {reason}")]
110    ImagePullFailed { image: String, reason: String },
111
112    #[error("Failed to parse image reference {image}: {reason}")]
113    ImageParseFailed { image: String, reason: String },
114
115    #[error("Image invalid: {0}")]
116    ImageInvalid(String),
117
118    #[error("Registry authentication failed: {0}")]
119    RegistryAuthFailed(String),
120
121    #[error("RAFS store error: {0}")]
122    RafsError(String),
123
124    // ─────────────────────────────────────────────────────────────────────
125    // Workflow Errors
126    // ─────────────────────────────────────────────────────────────────────
127    #[error("Workflow not found: {0}")]
128    WorkflowNotFound(String),
129
130    #[error("Workflow parse error: {0}")]
131    WorkflowParseError(String),
132
133    #[error("Workflow run not found: {0}")]
134    RunNotFound(String),
135
136    #[error("Workflow dispatch failed: {0}")]
137    DispatchFailed(String),
138
139    #[error("Workflow step failed: job={job}, step={step}, error={error}")]
140    StepFailed {
141        job: String,
142        step: String,
143        error: String,
144    },
145
146    #[error("Workflow timeout: {workflow_id} exceeded {timeout_secs}s")]
147    WorkflowTimeout {
148        workflow_id: String,
149        timeout_secs: u64,
150    },
151
152    // ─────────────────────────────────────────────────────────────────────
153    // Event Errors
154    // ─────────────────────────────────────────────────────────────────────
155    #[error("Event subscription failed: {0}")]
156    SubscriptionFailed(String),
157
158    #[error("Event publish failed: {0}")]
159    PublishFailed(String),
160
161    #[error("Invalid event topic: {0}")]
162    InvalidTopic(String),
163
164    // ─────────────────────────────────────────────────────────────────────
165    // Policy Errors
166    // ─────────────────────────────────────────────────────────────────────
167    #[error("Unauthorized: {subject} cannot {operation} on {resource}")]
168    Unauthorized {
169        subject: String,
170        operation: String,
171        resource: String,
172    },
173
174    #[error("Policy check failed: {0}")]
175    PolicyError(String),
176
177    // ─────────────────────────────────────────────────────────────────────
178    // Infrastructure Errors
179    // ─────────────────────────────────────────────────────────────────────
180    #[error("Transport error: {0}")]
181    TransportError(String),
182
183    #[error("Serialization error: {0}")]
184    SerializationError(String),
185
186    #[error("IO error: {0}")]
187    IoError(String),
188
189    #[error("Configuration error: {0}")]
190    ConfigError(String),
191
192    #[error("Internal error: {0}")]
193    Internal(String),
194
195    // ─────────────────────────────────────────────────────────────────────
196    // Namespace delivery errors (#635)
197    // ─────────────────────────────────────────────────────────────────────
198    /// A `SandboxBackend` operation (e.g. `deliver_namespace`) was invoked in
199    /// a shape the backend doesn't (yet) support — e.g. an unsupported
200    /// `NamespaceTransport` variant, or a backend that hasn't been re-grounded
201    /// against the narrowed trait surface yet.
202    #[error("Unsupported operation: {0}")]
203    Unsupported(String),
204}
205
206impl From<anyhow::Error> for WorkerError {
207    fn from(err: anyhow::Error) -> Self {
208        WorkerError::Internal(err.to_string())
209    }
210}
211
212impl From<serde_json::Error> for WorkerError {
213    fn from(err: serde_json::Error) -> Self {
214        WorkerError::SerializationError(err.to_string())
215    }
216}
217
218impl From<serde_yaml::Error> for WorkerError {
219    fn from(err: serde_yaml::Error) -> Self {
220        WorkerError::SerializationError(err.to_string())
221    }
222}
223
224impl From<capnp::Error> for WorkerError {
225    fn from(err: capnp::Error) -> Self {
226        WorkerError::SerializationError(err.to_string())
227    }
228}
229
230impl From<std::io::Error> for WorkerError {
231    fn from(err: std::io::Error) -> Self {
232        WorkerError::IoError(err.to_string())
233    }
234}
235
236/// Result type alias for worker operations
237pub type Result<T> = std::result::Result<T, WorkerError>;
238
239#[cfg(test)]
240mod tests {
241    use super::*;
242
243    #[test]
244    fn test_error_display() {
245        let err = WorkerError::SandboxNotFound("abc123".to_owned());
246        assert_eq!(err.to_string(), "Sandbox not found: abc123");
247
248        let err = WorkerError::Unauthorized {
249            subject: "anonymous".to_owned(),
250            operation: "execute".to_owned(),
251            resource: "sandbox:*".to_owned(),
252        };
253        assert!(err.to_string().contains("anonymous"));
254        assert!(err.to_string().contains("execute"));
255    }
256}