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    #[error("{0}")]
36    Other(#[from] anyhow::Error),
37}
38
39impl ClientError {
40    pub fn from_response(status: u16, body: String) -> Self {
41        Self::ApiError {
42            status,
43            details: Some(body.clone()),
44            message: body,
45        }
46    }
47
48    pub const fn is_retryable(&self) -> bool {
49        matches!(
50            self,
51            Self::Timeout | Self::ServerUnavailable(_) | Self::HttpError(_)
52        )
53    }
54}