Skip to main content

feagi_structures/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4/// Common error type for FEAGI data operations.
5///
6/// Provides structured error handling for serialization, deserialization,
7/// validation, and internal errors across the FEAGI data processing pipeline.
8///
9/// # Examples
10/// ```
11/// use feagi_structures::FeagiDataError;
12///
13/// fn validate_count(count: u32) -> Result<(), FeagiDataError> {
14///     if count == 0 {
15///         return Err(FeagiDataError::BadParameters("Count must be > 0".into()));
16///     }
17///     Ok(())
18/// }
19///
20/// assert!(validate_count(0).is_err());
21/// assert!(validate_count(5).is_ok());
22/// ```
23#[derive(Debug)]
24pub enum FeagiDataError {
25    /// Failed to deserialize bytes into data structures
26    DeserializationError(String),
27    /// Failed to serialize data structures into bytes
28    SerializationError(String),
29    /// Invalid parameters provided to a function
30    BadParameters(String),
31    /// Error related to neuron operations
32    NeuronError(String),
33    /// Internal error indicating a bug (please report)
34    InternalError(String),
35    /// resource is locked while system is running
36    ResourceLockedWhileRunning(String),
37    /// failed to process something in a const function
38    ConstError(&'static str),
39    /// Feature not yet implemented
40    NotImplemented,
41}
42
43impl Display for FeagiDataError {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        match self {
46            FeagiDataError::DeserializationError(msg) => {
47                write!(f, "Failed to Deserialize Bytes: {}", msg)
48            }
49            FeagiDataError::SerializationError(msg) => {
50                write!(f, "Failed to Serialize Bytes: {}", msg)
51            }
52            FeagiDataError::BadParameters(msg) => write!(f, "Bad Parameters: {}", msg),
53            FeagiDataError::NeuronError(msg) => write!(f, "NeuronError: {}", msg),
54            FeagiDataError::InternalError(msg) => write!(
55                f,
56                "Internal Error, please raise an issue on Github: {}",
57                msg
58            ),
59            FeagiDataError::ResourceLockedWhileRunning(msg) => {
60                write!(f, "Resource Locked While Running: {}", msg)
61            }
62            FeagiDataError::ConstError(msg) => write!(f, "ConstError: {}", msg),
63            FeagiDataError::NotImplemented => write!(
64                f,
65                "This function is not yet implemented! Please raise an issue on Github!"
66            ),
67        }
68    }
69}
70impl Error for FeagiDataError {}
71
72//  TODO From<> from other error types