Skip to main content

a2a_rs/adapter/error/
client.rs

1//! Error types for client adapters
2
3use crate::adapter::error::describe_transport_error;
4use crate::domain::A2AError;
5use std::io;
6use thiserror::Error;
7
8/// Error type for HTTP client adapter
9#[derive(Error, Debug)]
10#[cfg(any(feature = "http-client", feature = "jsonrpc-client"))]
11pub enum HttpClientError {
12    /// Reqwest client error
13    #[error("HTTP client error: {0}")]
14    Reqwest(#[from] reqwest::Error),
15
16    /// IO error during HTTP operations
17    #[error("IO error: {0}")]
18    Io(#[from] io::Error),
19
20    /// Error during request processing
21    #[error("Request error: {0}")]
22    Request(String),
23
24    /// Error with HTTP response
25    #[error("Response error: {status} - {message}")]
26    Response { status: u16, message: String },
27
28    /// Connection timeout
29    #[error("Connection timeout")]
30    Timeout,
31}
32
33// Conversion from adapter errors to domain errors
34#[cfg(any(feature = "http-client", feature = "jsonrpc-client"))]
35impl From<HttpClientError> for A2AError {
36    fn from(error: HttpClientError) -> Self {
37        match error {
38            // Flattened to a string here, so the cause has to come with it —
39            // `reqwest::Error` prints none of it on its own.
40            HttpClientError::Reqwest(e) => A2AError::Internal(format!(
41                "HTTP client error: {}",
42                describe_transport_error(&e)
43            )),
44            HttpClientError::Io(e) => A2AError::Io(e),
45            HttpClientError::Request(msg) => {
46                A2AError::Internal(format!("HTTP request error: {}", msg))
47            }
48            HttpClientError::Response { status, message } => {
49                A2AError::Internal(format!("HTTP response error: {} - {}", status, message))
50            }
51            HttpClientError::Timeout => A2AError::Internal("HTTP request timeout".to_string()),
52        }
53    }
54}