Skip to main content

dakera_client/
error.rs

1//! Error types for the Dakera client SDK
2
3use thiserror::Error;
4
5/// Result type alias for Dakera client operations
6pub type Result<T> = std::result::Result<T, ClientError>;
7
8/// Errors that can occur when using the Dakera client
9#[derive(Error, Debug)]
10pub enum ClientError {
11    /// HTTP request failed
12    #[cfg(feature = "http-client")]
13    #[error("HTTP request failed: {0}")]
14    Http(#[from] reqwest::Error),
15
16    /// gRPC request failed
17    #[cfg(feature = "grpc")]
18    #[error("gRPC request failed: {0}")]
19    Grpc(String),
20
21    /// JSON serialization/deserialization failed
22    #[error("JSON error: {0}")]
23    Json(#[from] serde_json::Error),
24
25    /// Server returned an error response
26    #[error("Server error ({status}): {message}")]
27    Server {
28        /// HTTP status code
29        status: u16,
30        /// Error message from server
31        message: String,
32    },
33
34    /// Invalid configuration
35    #[error("Invalid configuration: {0}")]
36    Config(String),
37
38    /// Namespace not found
39    #[error("Namespace not found: {0}")]
40    NamespaceNotFound(String),
41
42    /// Vector not found
43    #[error("Vector not found: {0}")]
44    VectorNotFound(String),
45
46    /// Invalid URL
47    #[error("Invalid URL: {0}")]
48    InvalidUrl(String),
49
50    /// Connection failed
51    #[error("Connection failed: {0}")]
52    Connection(String),
53
54    /// Timeout
55    #[error("Request timeout")]
56    Timeout,
57}
58
59impl ClientError {
60    /// Check if the error is retryable
61    pub fn is_retryable(&self) -> bool {
62        match self {
63            #[cfg(feature = "http-client")]
64            ClientError::Http(e) => e.is_timeout() || e.is_connect(),
65            #[cfg(feature = "grpc")]
66            ClientError::Grpc(_) => true, // gRPC errors are generally retryable
67            ClientError::Server { status, .. } => *status >= 500,
68            ClientError::Connection(_) => true,
69            ClientError::Timeout => true,
70            _ => false,
71        }
72    }
73
74    /// Check if the error is a not found error
75    pub fn is_not_found(&self) -> bool {
76        match self {
77            ClientError::Server { status, .. } => *status == 404,
78            ClientError::NamespaceNotFound(_) => true,
79            ClientError::VectorNotFound(_) => true,
80            _ => false,
81        }
82    }
83}