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("not found: {0}")]
53 NotFound(String),
54
55 #[error("operation failed: {0}")]
57 OperationFailed(String),
58
59 #[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 pub fn is_retryable(&self) -> bool {
73 matches!(
74 self,
75 Self::Connection(_) | Self::Transport(_) | Self::Timeout(_)
76 )
77 }
78
79 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}