deepstore_client/
error.rs1pub type Result<T> = std::result::Result<T, DeepstoreError>;
4
5#[derive(Debug, thiserror::Error)]
7pub enum DeepstoreError {
8 #[error("Authentication failed: {0}")]
10 Authentication(String),
11
12 #[error("Not found: {0}")]
14 NotFound(String),
15
16 #[error("Invalid request: {0}")]
18 InvalidRequest(String),
19
20 #[error("Server error: {0}")]
22 ServerError(String),
23
24 #[error("Network error: {0}")]
26 Network(String),
27
28 #[error("Failed to parse response: {0}")]
30 Parse(String),
31
32 #[error("Configuration error: {0}")]
34 Configuration(String),
35
36 #[error("Stream error: {0}")]
38 Stream(String),
39}
40
41impl DeepstoreError {
42 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}