Skip to main content

vmi_core/
error.rs

1use crate::AddressContext;
2
3/// An error that can occur when working with the VMI.
4#[derive(thiserror::Error, Debug)]
5pub enum VmiError {
6    /// An error occurred in the VMI driver.
7    #[error(transparent)]
8    Driver(Box<dyn std::error::Error + Send + Sync>),
9
10    /// An OS-specific error occurred.
11    #[error(transparent)]
12    Os(Box<dyn std::error::Error + Send + Sync>),
13
14    /// An I/O error occurred.
15    #[error(transparent)]
16    Io(#[from] std::io::Error),
17
18    /// An error occurred while parsing symbols.
19    #[error(transparent)]
20    Isr(#[from] isr_macros::Error),
21
22    /// An error occurred while working with the ISR cache.
23    #[error(transparent)]
24    IsrCache(#[from] isr_cache::Error),
25
26    /// A translation error occurred.
27    #[error("translation error ({:?})", .0)]
28    Translation(AddressContext),
29
30    /// The given address has invalid width.
31    #[error("invalid address width")]
32    InvalidAddressWidth,
33
34    /// Invalid timeout.
35    #[error("invalid timeout")]
36    InvalidTimeout,
37
38    /// Operation not supported.
39    #[error("operation not supported")]
40    NotSupported,
41
42    /// Out of bounds.
43    #[error("out of bounds")]
44    OutOfBounds,
45
46    /// Translation root not present.
47    #[error("translation root not present")]
48    RootNotPresent,
49
50    /// Operation timed out.
51    #[error("operation timed out")]
52    Timeout,
53
54    /// The view was not found.
55    #[error("view not found")]
56    ViewNotFound,
57
58    /// Other error.
59    #[error("{0}")]
60    Other(&'static str),
61}
62
63impl VmiError {
64    /// Boxes a driver-specific error into [`VmiError::Driver`].
65    pub fn driver<E>(err: E) -> Self
66    where
67        E: std::error::Error + Send + Sync + 'static,
68    {
69        Self::Driver(Box::new(err))
70    }
71
72    /// Boxes an OS-specific error into [`VmiError::Os`].
73    pub fn os<E>(err: E) -> Self
74    where
75        E: std::error::Error + Send + Sync + 'static,
76    {
77        Self::Os(Box::new(err))
78    }
79
80    /// Creates a new translation error.
81    pub fn translation(pf: impl Into<AddressContext>) -> Self {
82        Self::Translation(pf.into())
83    }
84}