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: {0}")]
21    AuthError(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: {0}")]
33    ConfigError(String),
34}
35
36impl ClientError {
37    pub fn from_response(status: u16, body: String) -> Self {
38        Self::ApiError {
39            status,
40            details: Some(body.clone()),
41            message: body,
42        }
43    }
44
45    pub const fn is_retryable(&self) -> bool {
46        matches!(
47            self,
48            Self::Timeout | Self::ServerUnavailable(_) | Self::HttpError(_)
49        )
50    }
51}