use thiserror::Error;
use entelix_core::error::Error;
pub type ToolResult<T> = std::result::Result<T, ToolError>;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ToolError {
#[error("invalid input: {0}")]
InvalidInput(String),
#[error("host '{host}' not on the allowlist")]
HostBlocked {
host: String,
},
#[error("unsupported URL scheme '{scheme}': only http/https are allowed")]
UnsupportedScheme {
scheme: String,
},
#[error("method '{method}' not allowed by this tool")]
MethodBlocked {
method: String,
},
#[error("response body exceeded {limit_bytes} bytes")]
BodyTooLarge {
limit_bytes: usize,
},
#[error("network: {message}")]
Network {
message: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
},
#[error("configuration: {message}")]
Config {
message: String,
#[source]
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
},
#[error("calculator: {0}")]
Calculator(String),
#[error(transparent)]
Serde(#[from] serde_json::Error),
}
impl ToolError {
pub fn network<E>(source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Network {
message: source.to_string(),
source: Some(Box::new(source)),
}
}
pub fn network_msg(message: impl Into<String>) -> Self {
Self::Network {
message: message.into(),
source: None,
}
}
pub fn config<E>(source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Config {
message: source.to_string(),
source: Some(Box::new(source)),
}
}
pub fn config_msg(message: impl Into<String>) -> Self {
Self::Config {
message: message.into(),
source: None,
}
}
}
impl From<ToolError> for Error {
fn from(err: ToolError) -> Self {
match err {
ToolError::Config { .. } => Self::config(err.to_string()),
ToolError::BodyTooLarge { .. } => Self::provider_http_from(413, err),
ToolError::Network { .. } => Self::provider_network_from(err),
ToolError::InvalidInput(_)
| ToolError::HostBlocked { .. }
| ToolError::UnsupportedScheme { .. }
| ToolError::MethodBlocked { .. }
| ToolError::Calculator(_)
| ToolError::Serde(_) => Self::invalid_request(err.to_string()),
}
}
}