Skip to main content

systemprompt_client/
error.rs

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