term_guard/analyzers/
errors.rs

1//! Error types for the analyzer framework.
2
3use thiserror::Error;
4
5/// Result type for analyzer operations.
6pub type AnalyzerResult<T> = Result<T, AnalyzerError>;
7
8/// Errors that can occur during analyzer operations.
9#[derive(Error, Debug)]
10pub enum AnalyzerError {
11    /// Error occurred while computing state from data.
12    #[error("Failed to compute state: {0}")]
13    StateComputation(String),
14
15    /// Error occurred while computing metric from state.
16    #[error("Failed to compute metric: {0}")]
17    MetricComputation(String),
18
19    /// Error occurred while merging states.
20    #[error("Failed to merge states: {0}")]
21    StateMerge(String),
22
23    /// DataFusion query execution error.
24    #[error("Query execution failed: {0}")]
25    QueryExecution(#[from] datafusion::error::DataFusionError),
26
27    /// Arrow computation error.
28    #[error("Arrow computation failed: {0}")]
29    ArrowComputation(#[from] arrow::error::ArrowError),
30
31    /// Invalid configuration or parameters.
32    #[error("Invalid configuration: {0}")]
33    InvalidConfiguration(String),
34
35    /// Data type mismatch or invalid data.
36    #[error("Invalid data: {0}")]
37    InvalidData(String),
38
39    /// No data available for analysis.
40    #[error("No data available for analysis")]
41    NoData,
42
43    /// Serialization/deserialization error.
44    #[error("Serialization error: {0}")]
45    Serialization(String),
46
47    /// Generic analyzer error with custom message.
48    #[error("{0}")]
49    Custom(String),
50}
51
52impl AnalyzerError {
53    /// Creates a state computation error with the given message.
54    pub fn state_computation(msg: impl Into<String>) -> Self {
55        Self::StateComputation(msg.into())
56    }
57
58    /// Creates a metric computation error with the given message.
59    pub fn metric_computation(msg: impl Into<String>) -> Self {
60        Self::MetricComputation(msg.into())
61    }
62
63    /// Creates a state merge error with the given message.
64    pub fn state_merge(msg: impl Into<String>) -> Self {
65        Self::StateMerge(msg.into())
66    }
67
68    /// Creates an invalid configuration error with the given message.
69    pub fn invalid_config(msg: impl Into<String>) -> Self {
70        Self::InvalidConfiguration(msg.into())
71    }
72
73    /// Creates an invalid data error with the given message.
74    pub fn invalid_data(msg: impl Into<String>) -> Self {
75        Self::InvalidData(msg.into())
76    }
77
78    /// Creates a custom error with the given message.
79    pub fn custom(msg: impl Into<String>) -> Self {
80        Self::Custom(msg.into())
81    }
82
83    /// Creates an execution error with the given message.
84    pub fn execution(msg: impl Into<String>) -> Self {
85        Self::Custom(format!("Execution error: {}", msg.into()))
86    }
87
88    /// Creates an internal error with the given message.
89    pub fn internal(msg: impl Into<String>) -> Self {
90        Self::Custom(format!("Internal error: {}", msg.into()))
91    }
92}
93
94/// Converts serde_json errors to AnalyzerError.
95impl From<serde_json::Error> for AnalyzerError {
96    fn from(err: serde_json::Error) -> Self {
97        Self::Serialization(err.to_string())
98    }
99}