Skip to main content

ninja/common/
error.rs

1// Simplified error handling with automatic conversions
2#[derive(Debug)]
3pub enum ShurikenError {
4    ServiceNotFound(String),
5    SpawnFailed(String, std::io::Error),
6    NoPid,
7    ConfigError(String),
8    ShurikensDirectoryNotFound,
9    InvalidServiceName,
10    ConfigParseError(String, toml::de::Error),
11    IoError(std::io::Error),
12}
13
14impl std::fmt::Display for ShurikenError {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            ShurikenError::ServiceNotFound(name) => write!(f, "Service '{}' not found", name),
18            ShurikenError::SpawnFailed(name, err) => {
19                write!(f, "Failed to spawn service '{}': {}", name, err)
20            }
21            ShurikenError::NoPid => write!(f, "Could not get process ID"),
22            ShurikenError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
23            ShurikenError::ShurikensDirectoryNotFound => write!(f, "Shurikens directory not found"),
24            ShurikenError::InvalidServiceName => write!(f, "Invalid service name"),
25            ShurikenError::ConfigParseError(name, err) => {
26                write!(f, "Failed to parse config for '{}': {}", name, err)
27            }
28            ShurikenError::IoError(err) => write!(f, "IO error: {}", err),
29        }
30    }
31}
32
33impl std::error::Error for ShurikenError {}
34
35// Automatic error conversions
36impl From<std::io::Error> for ShurikenError {
37    fn from(err: std::io::Error) -> Self {
38        ShurikenError::IoError(err)
39    }
40}