1use thiserror::Error;
2
3#[derive(Error, Debug)]
5pub enum BoxError {
6 #[error("VM boot failed: {message}")]
8 BoxBootError {
9 message: String,
10 hint: Option<String>,
11 },
12
13 #[error("Timeout: {0}")]
15 TimeoutError(String),
16
17 #[error("I/O error: {0}")]
19 IoError(#[from] std::io::Error),
20
21 #[error("Serialization error: {0}")]
23 SerializationError(String),
24
25 #[error("Configuration error: {0}")]
27 ConfigError(String),
28
29 #[error("TEE configuration error: {0}")]
31 TeeConfig(String),
32
33 #[error("TEE hardware not available: {0}")]
35 TeeNotSupported(String),
36
37 #[error("Attestation error: {0}")]
39 AttestationError(String),
40
41 #[error("OCI image error: {0}")]
43 OciImageError(String),
44
45 #[error("Registry error: {registry} - {message}")]
47 RegistryError { registry: String, message: String },
48
49 #[error("Cache error: {0}")]
51 CacheError(String),
52
53 #[error("Pool error: {0}")]
55 PoolError(String),
56
57 #[error("Exec error: {0}")]
59 ExecError(String),
60
61 #[error("Build error: {0}")]
63 BuildError(String),
64
65 #[error("Network error: {0}")]
67 NetworkError(String),
68
69 #[error("VM state error: {0}")]
71 StateError(String),
72
73 #[error("Audit error: {0}")]
75 AuditError(String),
76
77 #[error("Resize error: {0}")]
79 ResizeError(String),
80
81 #[error("{0}")]
83 Other(String),
84}
85
86impl From<serde_json::Error> for BoxError {
87 fn from(err: serde_json::Error) -> Self {
88 BoxError::SerializationError(err.to_string())
89 }
90}
91
92impl From<serde_yaml::Error> for BoxError {
93 fn from(err: serde_yaml::Error) -> Self {
94 BoxError::SerializationError(err.to_string())
95 }
96}
97
98pub type Result<T> = std::result::Result<T, BoxError>;
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn test_box_boot_error_display() {
107 let error = BoxError::BoxBootError {
108 message: "Failed to start VM".to_string(),
109 hint: Some("Check virtualization support".to_string()),
110 };
111 assert_eq!(error.to_string(), "VM boot failed: Failed to start VM");
112 }
113
114 #[test]
115 fn test_box_boot_error_without_hint() {
116 let error = BoxError::BoxBootError {
117 message: "No kernel found".to_string(),
118 hint: None,
119 };
120 assert_eq!(error.to_string(), "VM boot failed: No kernel found");
121 }
122
123 #[test]
124 fn test_timeout_error_display() {
125 let error = BoxError::TimeoutError("Operation timed out after 30s".to_string());
126 assert_eq!(error.to_string(), "Timeout: Operation timed out after 30s");
127 }
128
129 #[test]
130 fn test_io_error_conversion() {
131 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
132 let box_error: BoxError = io_error.into();
133 assert!(matches!(box_error, BoxError::IoError(_)));
134 assert!(box_error.to_string().contains("file not found"));
135 }
136
137 #[test]
138 fn test_serialization_error_display() {
139 let error = BoxError::SerializationError("Invalid JSON".to_string());
140 assert_eq!(error.to_string(), "Serialization error: Invalid JSON");
141 }
142
143 #[test]
144 fn test_config_error_display() {
145 let error = BoxError::ConfigError("Missing required field".to_string());
146 assert_eq!(
147 error.to_string(),
148 "Configuration error: Missing required field"
149 );
150 }
151
152 #[test]
153 fn test_other_error_display() {
154 let error = BoxError::Other("Unknown error occurred".to_string());
155 assert_eq!(error.to_string(), "Unknown error occurred");
156 }
157
158 #[test]
159 fn test_tee_config_error_display() {
160 let error = BoxError::TeeConfig("Failed to set TEE config file".to_string());
161 assert_eq!(
162 error.to_string(),
163 "TEE configuration error: Failed to set TEE config file"
164 );
165 }
166
167 #[test]
168 fn test_tee_not_supported_error_display() {
169 let error = BoxError::TeeNotSupported("AMD SEV-SNP not available".to_string());
170 assert_eq!(
171 error.to_string(),
172 "TEE hardware not available: AMD SEV-SNP not available"
173 );
174 }
175
176 #[test]
177 fn test_attestation_error_display() {
178 let error = BoxError::AttestationError("Failed to get SNP report".to_string());
179 assert_eq!(
180 error.to_string(),
181 "Attestation error: Failed to get SNP report"
182 );
183 }
184
185 #[test]
186 fn test_oci_image_error_display() {
187 let error = BoxError::OciImageError("Invalid manifest".to_string());
188 assert_eq!(error.to_string(), "OCI image error: Invalid manifest");
189 }
190
191 #[test]
192 fn test_registry_error_display() {
193 let error = BoxError::RegistryError {
194 registry: "ghcr.io".to_string(),
195 message: "Authentication failed".to_string(),
196 };
197 assert_eq!(
198 error.to_string(),
199 "Registry error: ghcr.io - Authentication failed"
200 );
201 }
202
203 #[test]
204 fn test_serde_json_error_conversion() {
205 let json_str = "{ invalid json }";
206 let result: std::result::Result<serde_json::Value, _> = serde_json::from_str(json_str);
207 let json_error = result.unwrap_err();
208 let box_error: BoxError = json_error.into();
209 assert!(matches!(box_error, BoxError::SerializationError(_)));
210 }
211
212 #[test]
213 fn test_serde_yaml_error_conversion() {
214 let yaml_str = "invalid: yaml: content:";
215 let result: std::result::Result<serde_yaml::Value, _> = serde_yaml::from_str(yaml_str);
216 let yaml_error = result.unwrap_err();
217 let box_error: BoxError = yaml_error.into();
218 assert!(matches!(box_error, BoxError::SerializationError(_)));
219 }
220
221 #[test]
222 fn test_result_type_alias() {
223 fn returns_ok() -> Result<i32> {
224 Ok(42)
225 }
226
227 fn returns_err() -> Result<i32> {
228 Err(BoxError::Other("test error".to_string()))
229 }
230
231 assert_eq!(returns_ok().unwrap(), 42);
232 assert!(returns_err().is_err());
233 }
234
235 #[test]
236 fn test_resize_error_display() {
237 let error = BoxError::ResizeError("Cannot change vCPU count".to_string());
238 assert_eq!(error.to_string(), "Resize error: Cannot change vCPU count");
239 }
240
241 #[test]
242 fn test_error_is_debug() {
243 let error = BoxError::Other("test".to_string());
244 let debug_str = format!("{:?}", error);
245 assert!(debug_str.contains("Other"));
246 }
247
248 #[test]
249 fn test_cache_error_display() {
250 let error = BoxError::CacheError("Rootfs cache corrupted".to_string());
251 assert_eq!(error.to_string(), "Cache error: Rootfs cache corrupted");
252 }
253
254 #[test]
255 fn test_pool_error_display() {
256 let error = BoxError::PoolError("No idle VMs available".to_string());
257 assert_eq!(error.to_string(), "Pool error: No idle VMs available");
258 }
259
260 #[test]
261 fn test_exec_error_display() {
262 let error = BoxError::ExecError("Command not found".to_string());
263 assert_eq!(error.to_string(), "Exec error: Command not found");
264 }
265
266 #[test]
267 fn test_build_error_display() {
268 let error = BoxError::BuildError("Dockerfile parse failed".to_string());
269 assert_eq!(error.to_string(), "Build error: Dockerfile parse failed");
270 }
271}