d_engine_client/
kv_error.rs

1//! Error types for KV client operations.
2//!
3//! Provides unified error handling for both remote (gRPC) and embedded (local)
4//! KV client implementations.
5
6use crate::ClientApiError;
7
8/// Result type for KV client operations
9pub type KvResult<T> = std::result::Result<T, KvClientError>;
10
11/// Error types for KV client operations
12#[derive(Debug, Clone)]
13pub enum KvClientError {
14    /// Channel closed (node shutdown or connection lost)
15    ChannelClosed,
16
17    /// Operation timeout
18    Timeout,
19
20    /// Server returned error (e.g., not leader, storage error)
21    ServerError(String),
22
23    /// Network error (only for remote clients)
24    NetworkError(String),
25
26    /// Invalid argument
27    InvalidArgument(String),
28}
29
30impl std::fmt::Display for KvClientError {
31    fn fmt(
32        &self,
33        f: &mut std::fmt::Formatter<'_>,
34    ) -> std::fmt::Result {
35        match self {
36            KvClientError::ChannelClosed => write!(f, "Channel closed"),
37            KvClientError::Timeout => write!(f, "Operation timeout"),
38            KvClientError::ServerError(msg) => write!(f, "Server error: {msg}"),
39            KvClientError::NetworkError(msg) => write!(f, "Network error: {msg}"),
40            KvClientError::InvalidArgument(msg) => write!(f, "Invalid argument: {msg}"),
41        }
42    }
43}
44
45impl std::error::Error for KvClientError {}
46
47// ==================== Error Conversions ====================
48
49/// Convert ClientApiError to KvClientError
50impl From<ClientApiError> for KvClientError {
51    fn from(err: ClientApiError) -> Self {
52        match err {
53            ClientApiError::Network { message, .. } => KvClientError::NetworkError(message),
54            ClientApiError::Protocol { message, .. } => KvClientError::ServerError(message),
55            ClientApiError::Storage { message, .. } => KvClientError::ServerError(message),
56            ClientApiError::Business { code, message, .. } => {
57                // Check if it's a timeout or not-leader error
58                use d_engine_proto::error::ErrorCode;
59                match code {
60                    ErrorCode::ConnectionTimeout => KvClientError::Timeout,
61                    ErrorCode::NotLeader => KvClientError::ServerError(message),
62                    _ => KvClientError::ServerError(message),
63                }
64            }
65            ClientApiError::General { message, .. } => KvClientError::ServerError(message),
66        }
67    }
68}