amaters_sdk_rust/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, SdkError>;
7
8#[derive(Debug, Error)]
10pub enum SdkError {
11 #[error("connection error: {0}")]
13 Connection(String),
14
15 #[error("transport error: {0}")]
17 Transport(#[from] tonic::transport::Error),
18
19 #[error("gRPC error: {0}")]
21 Grpc(#[from] tonic::Status),
22
23 #[error("operation timeout: {0}")]
25 Timeout(String),
26
27 #[error("configuration error: {0}")]
29 Configuration(String),
30
31 #[error("serialization error: {0}")]
33 Serialization(String),
34
35 #[error("FHE error: {0}")]
37 Fhe(String),
38
39 #[error("core error: {0}")]
41 Core(#[from] amaters_core::AmateRSError),
42
43 #[error("network error: {0}")]
45 Network(#[from] amaters_net::NetError),
46
47 #[error("invalid argument: {0}")]
49 InvalidArgument(String),
50
51 #[error("invalid state: {0}")]
53 InvalidState(String),
54
55 #[error("not found: {0}")]
57 NotFound(String),
58
59 #[error("operation failed: {0}")]
61 OperationFailed(String),
62
63 #[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 pub fn is_retryable(&self) -> bool {
77 matches!(
78 self,
79 Self::Connection(_) | Self::Transport(_) | Self::Timeout(_)
80 )
81 }
82
83 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}