Skip to main content

deepstore_client/
error.rs

1//! Error types for DeepStore client
2
3pub type Result<T> = std::result::Result<T, DeepstoreError>;
4
5/// DeepStore client errors
6#[derive(Debug, thiserror::Error)]
7pub enum DeepstoreError {
8    /// Authentication failed (401/403)
9    #[error("Authentication failed: {0}")]
10    Authentication(String),
11
12    /// Resource not found (404)
13    #[error("Not found: {0}")]
14    NotFound(String),
15
16    /// Invalid request (400)
17    #[error("Invalid request: {0}")]
18    InvalidRequest(String),
19
20    /// Server error (500+)
21    #[error("Server error: {0}")]
22    ServerError(String),
23
24    /// Network/HTTP error
25    #[error("Network error: {0}")]
26    Network(String),
27
28    /// JSON parsing error
29    #[error("Failed to parse response: {0}")]
30    Parse(String),
31
32    /// Configuration error
33    #[error("Configuration error: {0}")]
34    Configuration(String),
35
36    /// Streaming error
37    #[error("Stream error: {0}")]
38    Stream(String),
39}
40
41impl DeepstoreError {
42    /// Create error from HTTP status and response body
43    pub fn from_response(status: reqwest::StatusCode, body: &str) -> Self {
44        match status.as_u16() {
45            401 | 403 => Self::Authentication(body.to_string()),
46            404 => Self::NotFound(body.to_string()),
47            400 | 422 => Self::InvalidRequest(body.to_string()),
48            _ if status.is_server_error() => Self::ServerError(body.to_string()),
49            _ => Self::Network(format!("HTTP {}: {}", status, body)),
50        }
51    }
52}
53
54impl From<reqwest::Error> for DeepstoreError {
55    fn from(e: reqwest::Error) -> Self {
56        if e.is_timeout() {
57            Self::Network(format!("Request timeout: {}", e))
58        } else if e.is_connect() {
59            Self::Network(format!("Connection failed: {}", e))
60        } else {
61            Self::Network(e.to_string())
62        }
63    }
64}
65
66impl From<serde_json::Error> for DeepstoreError {
67    fn from(e: serde_json::Error) -> Self {
68        Self::Parse(e.to_string())
69    }
70}