Skip to main content

oxigdal_edge/
error.rs

1//! Error types for edge computing operations
2
3use std::fmt;
4
5/// Result type alias for edge operations
6pub type Result<T> = std::result::Result<T, EdgeError>;
7
8/// Edge computing errors
9#[derive(Debug, thiserror::Error)]
10pub enum EdgeError {
11    /// I/O error occurred
12    #[error("I/O error: {0}")]
13    Io(#[from] std::io::Error),
14
15    /// Serialization error
16    #[error("Serialization error: {0}")]
17    Serialization(String),
18
19    /// Deserialization error
20    #[error("Deserialization error: {0}")]
21    Deserialization(String),
22
23    /// Compression error
24    #[error("Compression error: {0}")]
25    Compression(String),
26
27    /// Decompression error
28    #[error("Decompression error: {0}")]
29    Decompression(String),
30
31    /// Cache error
32    #[error("Cache error: {0}")]
33    Cache(String),
34
35    /// Synchronization error
36    #[error("Sync error: {0}")]
37    Sync(String),
38
39    /// Conflict resolution error
40    #[error("Conflict resolution error: {0}")]
41    Conflict(String),
42
43    /// Resource constraint error
44    #[error("Resource constraint violated: {0}")]
45    ResourceConstraint(String),
46
47    /// Runtime error
48    #[error("Runtime error: {0}")]
49    Runtime(String),
50
51    /// Invalid configuration
52    #[error("Invalid configuration: {0}")]
53    InvalidConfig(String),
54
55    /// Network error
56    #[error("Network error: {0}")]
57    Network(String),
58
59    /// Storage error
60    #[error("Storage error: {0}")]
61    Storage(String),
62
63    /// Operation not supported in current mode
64    #[error("Operation not supported: {0}")]
65    NotSupported(String),
66
67    /// Timeout error
68    #[error("Operation timed out: {0}")]
69    Timeout(String),
70
71    /// Generic error
72    #[error("{0}")]
73    Other(String),
74}
75
76impl EdgeError {
77    /// Create a new serialization error
78    pub fn serialization<S: fmt::Display>(msg: S) -> Self {
79        Self::Serialization(msg.to_string())
80    }
81
82    /// Create a new deserialization error
83    pub fn deserialization<S: fmt::Display>(msg: S) -> Self {
84        Self::Deserialization(msg.to_string())
85    }
86
87    /// Create a new compression error
88    pub fn compression<S: fmt::Display>(msg: S) -> Self {
89        Self::Compression(msg.to_string())
90    }
91
92    /// Create a new decompression error
93    pub fn decompression<S: fmt::Display>(msg: S) -> Self {
94        Self::Decompression(msg.to_string())
95    }
96
97    /// Create a new cache error
98    pub fn cache<S: fmt::Display>(msg: S) -> Self {
99        Self::Cache(msg.to_string())
100    }
101
102    /// Create a new sync error
103    pub fn sync<S: fmt::Display>(msg: S) -> Self {
104        Self::Sync(msg.to_string())
105    }
106
107    /// Create a new conflict error
108    pub fn conflict<S: fmt::Display>(msg: S) -> Self {
109        Self::Conflict(msg.to_string())
110    }
111
112    /// Create a new resource constraint error
113    pub fn resource_constraint<S: fmt::Display>(msg: S) -> Self {
114        Self::ResourceConstraint(msg.to_string())
115    }
116
117    /// Create a new runtime error
118    pub fn runtime<S: fmt::Display>(msg: S) -> Self {
119        Self::Runtime(msg.to_string())
120    }
121
122    /// Create a new invalid config error
123    pub fn invalid_config<S: fmt::Display>(msg: S) -> Self {
124        Self::InvalidConfig(msg.to_string())
125    }
126
127    /// Create a new network error
128    pub fn network<S: fmt::Display>(msg: S) -> Self {
129        Self::Network(msg.to_string())
130    }
131
132    /// Create a new storage error
133    pub fn storage<S: fmt::Display>(msg: S) -> Self {
134        Self::Storage(msg.to_string())
135    }
136
137    /// Create a new not supported error
138    pub fn not_supported<S: fmt::Display>(msg: S) -> Self {
139        Self::NotSupported(msg.to_string())
140    }
141
142    /// Create a new timeout error
143    pub fn timeout<S: fmt::Display>(msg: S) -> Self {
144        Self::Timeout(msg.to_string())
145    }
146
147    /// Create a new generic error
148    pub fn other<S: fmt::Display>(msg: S) -> Self {
149        Self::Other(msg.to_string())
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_error_creation() {
159        let err = EdgeError::cache("test error");
160        assert_eq!(err.to_string(), "Cache error: test error");
161
162        let err = EdgeError::sync("sync failed");
163        assert_eq!(err.to_string(), "Sync error: sync failed");
164
165        let err = EdgeError::resource_constraint("memory limit exceeded");
166        assert_eq!(
167            err.to_string(),
168            "Resource constraint violated: memory limit exceeded"
169        );
170    }
171
172    #[test]
173    fn test_error_conversion() {
174        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
175        let edge_err: EdgeError = io_err.into();
176        assert!(matches!(edge_err, EdgeError::Io(_)));
177    }
178}