Skip to main content

arcbox_hypervisor/
error.rs

1//! Error types for the hypervisor crate.
2
3use arcbox_error::CommonError;
4use thiserror::Error;
5
6/// Result type alias for hypervisor operations.
7pub type Result<T> = std::result::Result<T, HypervisorError>;
8
9/// Errors that can occur during hypervisor operations.
10#[derive(Debug, Error)]
11pub enum HypervisorError {
12    /// Common error from arcbox-error.
13    #[error(transparent)]
14    Common(#[from] CommonError),
15
16    /// Platform not supported.
17    #[error("platform not supported: {0}")]
18    UnsupportedPlatform(String),
19
20    /// Failed to initialize hypervisor.
21    #[error("failed to initialize hypervisor: {0}")]
22    InitializationFailed(String),
23
24    /// Failed to create virtual machine.
25    #[error("failed to create VM: {0}")]
26    VmCreationFailed(String),
27
28    /// Failed to create vCPU.
29    #[error("failed to create vCPU {id}: {reason}")]
30    VcpuCreationFailed { id: u32, reason: String },
31
32    /// Memory mapping error.
33    #[error("memory error: {0}")]
34    MemoryError(String),
35
36    /// VM not in expected state.
37    #[error("VM state error: expected {expected}, got {actual}")]
38    VmStateError { expected: String, actual: String },
39
40    /// vCPU execution error.
41    #[error("vCPU execution error: {0}")]
42    VcpuRunError(String),
43
44    /// Device error.
45    #[error("device error: {0}")]
46    DeviceError(String),
47
48    /// VM runtime error.
49    #[error("VM error: {0}")]
50    VmError(String),
51
52    /// Snapshot error.
53    #[error("snapshot error: {0}")]
54    SnapshotError(String),
55
56    /// Feature not supported.
57    #[error("not supported: {0}")]
58    NotSupported(String),
59
60    /// Platform-specific error.
61    #[cfg(target_os = "macos")]
62    #[error("darwin error: {0}")]
63    DarwinError(String),
64
65    /// Platform-specific error.
66    #[cfg(target_os = "linux")]
67    #[error("KVM error: {0}")]
68    KvmError(String),
69}
70
71impl HypervisorError {
72    /// Creates a timeout error via `CommonError`.
73    #[must_use]
74    pub fn timeout(msg: impl Into<String>) -> Self {
75        Self::Common(CommonError::timeout(msg))
76    }
77
78    /// Creates an invalid config error via `CommonError`.
79    #[must_use]
80    pub fn invalid_config(msg: impl Into<String>) -> Self {
81        Self::Common(CommonError::config(msg))
82    }
83}
84
85// Allow automatic conversion from std::io::Error to HypervisorError via CommonError.
86impl From<std::io::Error> for HypervisorError {
87    fn from(err: std::io::Error) -> Self {
88        Self::Common(CommonError::from(err))
89    }
90}