amaters_sdk_rust/
error.rs

1//! Error types for the AmateRS SDK
2
3use thiserror::Error;
4
5/// Result type for SDK operations
6pub type Result<T> = std::result::Result<T, SdkError>;
7
8/// SDK error types
9#[derive(Debug, Error)]
10pub enum SdkError {
11    /// Connection error
12    #[error("connection error: {0}")]
13    Connection(String),
14
15    /// Transport error (gRPC)
16    #[error("transport error: {0}")]
17    Transport(#[from] tonic::transport::Error),
18
19    /// gRPC status error
20    #[error("gRPC error: {0}")]
21    Grpc(#[from] tonic::Status),
22
23    /// Timeout error
24    #[error("operation timeout: {0}")]
25    Timeout(String),
26
27    /// Configuration error
28    #[error("configuration error: {0}")]
29    Configuration(String),
30
31    /// Serialization error
32    #[error("serialization error: {0}")]
33    Serialization(String),
34
35    /// FHE operation error
36    #[error("FHE error: {0}")]
37    Fhe(String),
38
39    /// Core library error
40    #[error("core error: {0}")]
41    Core(#[from] amaters_core::AmateRSError),
42
43    /// Network layer error
44    #[error("network error: {0}")]
45    Network(#[from] amaters_net::NetError),
46
47    /// Invalid argument
48    #[error("invalid argument: {0}")]
49    InvalidArgument(String),
50
51    /// Not found
52    #[error("not found: {0}")]
53    NotFound(String),
54
55    /// Operation failed
56    #[error("operation failed: {0}")]
57    OperationFailed(String),
58
59    /// Other error
60    #[error("error: {0}")]
61    Other(String),
62}
63
64impl From<anyhow::Error> for SdkError {
65    fn from(err: anyhow::Error) -> Self {
66        Self::Other(err.to_string())
67    }
68}
69
70impl SdkError {
71    /// Check if the error is retryable
72    pub fn is_retryable(&self) -> bool {
73        matches!(
74            self,
75            Self::Connection(_) | Self::Transport(_) | Self::Timeout(_)
76        )
77    }
78
79    /// Check if the error is a connection error
80    pub fn is_connection_error(&self) -> bool {
81        matches!(self, Self::Connection(_) | Self::Transport(_))
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_error_is_retryable() {
91        let err = SdkError::Connection("test".to_string());
92        assert!(err.is_retryable());
93
94        let err = SdkError::InvalidArgument("test".to_string());
95        assert!(!err.is_retryable());
96    }
97
98    #[test]
99    fn test_error_display() {
100        let err = SdkError::Connection("failed to connect".to_string());
101        assert_eq!(err.to_string(), "connection error: failed to connect");
102    }
103}