batata_client/
error.rs

1use thiserror::Error;
2
3/// Result type alias for batata client operations
4pub type Result<T> = std::result::Result<T, BatataError>;
5
6/// Error types for Batata client operations
7#[derive(Error, Debug)]
8pub enum BatataError {
9    /// Configuration not found
10    #[error("Config not found: dataId={data_id}, group={group}, namespace={namespace}")]
11    ConfigNotFound {
12        data_id: String,
13        group: String,
14        namespace: String,
15    },
16
17    /// Service not found
18    #[error("Service not found: service={service_name}, group={group_name}, namespace={namespace}")]
19    ServiceNotFound {
20        service_name: String,
21        group_name: String,
22        namespace: String,
23    },
24
25    /// Connection error
26    #[error("Connection error: {message}")]
27    ConnectionError { message: String },
28
29    /// Server error with error code
30    #[error("Server error: code={error_code}, message={message}")]
31    ServerError { error_code: i32, message: String },
32
33    /// Authentication error
34    #[error("Authentication error: {message}")]
35    AuthError { message: String },
36
37    /// Permission denied
38    #[error("Permission denied: {message}")]
39    PermissionDenied { message: String },
40
41    /// Request timeout
42    #[error("Request timeout after {timeout_ms}ms")]
43    Timeout { timeout_ms: u64 },
44
45    /// Serialization/Deserialization error
46    #[error("Serialization error: {0}")]
47    SerializationError(#[from] serde_json::Error),
48
49    /// gRPC transport error
50    #[error("gRPC transport error: {0}")]
51    TransportError(#[from] tonic::transport::Error),
52
53    /// gRPC status error
54    #[error("gRPC status error: {0}")]
55    GrpcStatus(#[from] tonic::Status),
56
57    /// Invalid parameter
58    #[error("Invalid parameter: {0}")]
59    InvalidParameter(String),
60
61    /// Internal error
62    #[error("Internal error: {0}")]
63    Internal(String),
64
65    /// No available server
66    #[error("No available server")]
67    NoAvailableServer,
68
69    /// Client not started
70    #[error("Client not started")]
71    ClientNotStarted,
72
73    /// Client already started
74    #[error("Client already started")]
75    ClientAlreadyStarted,
76
77    /// Encryption/Decryption error
78    #[error("Encryption error: {message}")]
79    EncryptionError { message: String },
80}
81
82impl BatataError {
83    /// Check if the error is retryable
84    pub fn is_retryable(&self) -> bool {
85        matches!(
86            self,
87            BatataError::ConnectionError { .. }
88                | BatataError::Timeout { .. }
89                | BatataError::TransportError(_)
90                | BatataError::NoAvailableServer
91        )
92    }
93
94    /// Create a server error from error code and message
95    pub fn server_error(error_code: i32, message: impl Into<String>) -> Self {
96        BatataError::ServerError {
97            error_code,
98            message: message.into(),
99        }
100    }
101
102    /// Create a connection error
103    pub fn connection_error(message: impl Into<String>) -> Self {
104        BatataError::ConnectionError {
105            message: message.into(),
106        }
107    }
108}