Skip to main content

systemprompt_client/
error.rs

1use thiserror::Error;
2
3pub type ClientResult<T> = Result<T, ClientError>;
4
5#[derive(Debug, Error)]
6pub enum ClientError {
7    #[error("HTTP request failed: {0}")]
8    HttpError(#[from] reqwest::Error),
9
10    #[error("API error: {status} - {message}")]
11    ApiError {
12        status: u16,
13        message: String,
14        details: Option<String>,
15    },
16
17    #[error("Failed to parse JSON: {0}")]
18    JsonError(#[from] serde_json::Error),
19
20    #[error("Authentication failed: {message}")]
21    AuthError { message: String },
22
23    #[error("Resource not found: {0}")]
24    NotFound(String),
25
26    #[error("Request timeout")]
27    Timeout,
28
29    #[error("Server unavailable: {0}")]
30    ServerUnavailable(String),
31
32    #[error("Invalid configuration: {message}")]
33    ConfigError { message: String },
34
35    #[error("Failed to create event stream")]
36    EventStreamSetup,
37
38    #[error("I/O error: {0}")]
39    Io(#[from] std::io::Error),
40}
41
42impl ClientError {
43    pub fn from_response(status: u16, body: String) -> Self {
44        Self::ApiError {
45            status,
46            details: Some(body.clone()),
47            message: body,
48        }
49    }
50
51    pub const fn is_retryable(&self) -> bool {
52        matches!(
53            self,
54            Self::Timeout | Self::ServerUnavailable(_) | Self::HttpError(_)
55        )
56    }
57}