use crate::adapter::error::describe_transport_error;
use crate::domain::A2AError;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
#[cfg(any(feature = "http-client", feature = "jsonrpc-client", feature = "auth"))]
pub enum HttpClientError {
#[error("HTTP client error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Request error: {0}")]
Request(String),
#[error("Response error: {status} - {message}")]
Response { status: u16, message: String },
#[error("Connection timeout")]
Timeout,
}
#[cfg(any(feature = "http-client", feature = "jsonrpc-client", feature = "auth"))]
pub(crate) fn http_client() -> Result<reqwest::Client, A2AError> {
reqwest::Client::builder()
.build()
.map_err(|e| HttpClientError::Reqwest(e).into())
}
#[cfg(any(feature = "http-client", feature = "jsonrpc-client", feature = "auth"))]
impl From<HttpClientError> for A2AError {
fn from(error: HttpClientError) -> Self {
match error {
HttpClientError::Reqwest(e) => A2AError::Internal(format!(
"HTTP client error: {}",
describe_transport_error(&e)
)),
HttpClientError::Io(e) => A2AError::Io(e),
HttpClientError::Request(msg) => {
A2AError::Internal(format!("HTTP request error: {}", msg))
}
HttpClientError::Response { status, message } => {
A2AError::Internal(format!("HTTP response error: {} - {}", status, message))
}
HttpClientError::Timeout => A2AError::Internal("HTTP request timeout".to_string()),
}
}
}