Skip to main content

oxideshield_core/
error.rs

1//! Error types for OxideShield
2
3use thiserror::Error;
4
5/// Result type alias for OxideShield operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Core error types for OxideShield
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Pattern compilation error
12    #[error("Failed to compile pattern: {0}")]
13    PatternCompilation(String),
14
15    /// Invalid pattern syntax
16    #[error("Invalid pattern syntax: {0}")]
17    InvalidPattern(String),
18
19    /// Configuration error
20    #[error("Configuration error: {0}")]
21    Config(String),
22
23    /// IO error
24    #[error("IO error: {0}")]
25    Io(#[from] std::io::Error),
26
27    /// YAML parsing error
28    #[error("YAML parsing error: {0}")]
29    Yaml(#[from] serde_saphyr::Error),
30
31    /// JSON parsing error
32    #[error("JSON parsing error: {0}")]
33    Json(#[from] serde_json::Error),
34
35    /// Regex error
36    #[error("Regex error: {0}")]
37    Regex(#[from] regex::Error),
38
39    /// Aho-Corasick error
40    #[error("Aho-Corasick build error: {0}")]
41    AhoCorasick(#[from] aho_corasick::BuildError),
42
43    /// Tokenizer error
44    #[error("Tokenizer error: {0}")]
45    Tokenizer(String),
46
47    /// Guard validation error
48    #[error("Guard validation failed: {0}")]
49    GuardValidation(String),
50
51    /// Probe execution error
52    #[error("Probe execution failed: {0}")]
53    ProbeExecution(String),
54
55    /// Network error
56    #[error("Network error: {0}")]
57    Network(String),
58
59    /// Generic error with context
60    #[error("{context}: {source}")]
61    WithContext {
62        context: String,
63        #[source]
64        source: Box<Error>,
65    },
66}
67
68impl Error {
69    /// Add context to an error
70    pub fn with_context(self, context: impl Into<String>) -> Self {
71        Error::WithContext {
72            context: context.into(),
73            source: Box::new(self),
74        }
75    }
76}