Skip to main content

edict/
error.rs

1use std::process::ExitCode;
2
3/// Errors that cause edict to exit with a specific code.
4#[derive(Debug, thiserror::Error)]
5pub enum ExitError {
6    #[error("config error: {0}")]
7    Config(String),
8
9    #[error("tool not found: {tool}")]
10    ToolNotFound { tool: String },
11
12    #[error("{tool} failed (exit {code}): {message}")]
13    ToolFailed {
14        tool: String,
15        code: i32,
16        message: String,
17    },
18
19    #[error("{tool} timed out after {timeout_secs}s")]
20    Timeout { tool: String, timeout_secs: u64 },
21
22    #[error("{message}")]
23    WithCode { code: u8, message: String },
24
25    #[error("audit failed")]
26    AuditFailed,
27
28    #[error("{0}")]
29    Other(String),
30}
31
32impl ExitError {
33    pub fn new(code: u8, message: String) -> Self {
34        ExitError::WithCode { code, message }
35    }
36
37    pub fn exit_code(&self) -> ExitCode {
38        match self {
39            ExitError::Config(_) => ExitCode::from(2),
40            ExitError::ToolNotFound { .. } => ExitCode::from(3),
41            ExitError::ToolFailed { .. } => ExitCode::from(4),
42            ExitError::Timeout { .. } => ExitCode::from(5),
43            ExitError::WithCode { code, .. } => ExitCode::from(*code),
44            ExitError::AuditFailed => ExitCode::from(6),
45            ExitError::Other(_) => ExitCode::from(1),
46        }
47    }
48}