Skip to main content

sandbox_run/
error.rs

1use std::{error::Error, fmt, io};
2
3#[derive(Debug)]
4pub enum SandboxError {
5    Io(io::Error),
6    #[cfg(target_os = "macos")]
7    MacOS(String),
8    #[cfg(target_os = "linux")]
9    Landlock(landlock::RulesetError),
10    InvalidPath(String),
11}
12
13impl fmt::Display for SandboxError {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        match self {
16            SandboxError::Io(e) => write!(f, "I/O error: {}", e),
17            #[cfg(target_os = "macos")]
18            SandboxError::MacOS(msg) => write!(f, "macOS sandbox error: {}", msg),
19            #[cfg(target_os = "linux")]
20            SandboxError::Landlock(e) => write!(f, "Landlock error: {}", e),
21            SandboxError::InvalidPath(msg) => write!(f, "Invalid path: {}", msg),
22        }
23    }
24}
25
26impl Error for SandboxError {
27    fn source(&self) -> Option<&(dyn Error + 'static)> {
28        match self {
29            SandboxError::Io(e) => Some(e),
30            #[cfg(target_os = "linux")]
31            SandboxError::Landlock(e) => Some(e),
32            _ => None,
33        }
34    }
35}
36
37impl From<io::Error> for SandboxError {
38    fn from(e: io::Error) -> Self {
39        SandboxError::Io(e)
40    }
41}
42
43#[cfg(target_os = "linux")]
44impl From<landlock::RulesetError> for SandboxError {
45    fn from(e: landlock::RulesetError) -> Self {
46        SandboxError::Landlock(e)
47    }
48}