Skip to main content

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    /// Invalid state transition (e.g. double-commit, rollback after commit)
52    #[error("invalid state: {0}")]
53    InvalidState(String),
54
55    /// Not found
56    #[error("not found: {0}")]
57    NotFound(String),
58
59    /// Operation failed
60    #[error("operation failed: {0}")]
61    OperationFailed(String),
62
63    /// Other error
64    #[error("error: {0}")]
65    Other(String),
66}
67
68impl From<anyhow::Error> for SdkError {
69    fn from(err: anyhow::Error) -> Self {
70        Self::Other(err.to_string())
71    }
72}
73
74impl SdkError {
75    /// Check if the error is retryable
76    pub fn is_retryable(&self) -> bool {
77        matches!(
78            self,
79            Self::Connection(_) | Self::Transport(_) | Self::Timeout(_)
80        )
81    }
82
83    /// Check if the error is a connection error
84    pub fn is_connection_error(&self) -> bool {
85        matches!(self, Self::Connection(_) | Self::Transport(_))
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_error_is_retryable() {
95        let err = SdkError::Connection("test".to_string());
96        assert!(err.is_retryable());
97
98        let err = SdkError::InvalidArgument("test".to_string());
99        assert!(!err.is_retryable());
100    }
101
102    #[test]
103    fn test_error_display() {
104        let err = SdkError::Connection("failed to connect".to_string());
105        assert_eq!(err.to_string(), "connection error: failed to connect");
106    }
107}