80 lines
2.0 KiB
Rust
Raw Normal View History

use thiserror::Error;
#[derive(Error, Debug)]
pub enum EmbeddingError {
#[error("Provider initialization failed: {0}")]
Initialization(String),
#[error("Provider not available: {0}")]
ProviderUnavailable(String),
#[error("API request failed: {0}")]
ApiError(String),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch { expected: usize, actual: usize },
#[error("Batch size {size} exceeds maximum {max}")]
BatchSizeExceeded { size: usize, max: usize },
#[error("Cache error: {0}")]
CacheError(String),
#[error("Store error: {0}")]
StoreError(String),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Rate limit exceeded: {0}")]
RateLimitExceeded(String),
#[error("Timeout: {0}")]
Timeout(String),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("IO error: {0}")]
IoError(String),
#[error("HTTP error: {0}")]
HttpError(String),
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}
impl From<std::io::Error> for EmbeddingError {
fn from(err: std::io::Error) -> Self {
Self::IoError(err.to_string())
}
}
impl From<serde_json::Error> for EmbeddingError {
fn from(err: serde_json::Error) -> Self {
Self::SerializationError(err.to_string())
}
}
#[cfg(feature = "reqwest")]
impl From<reqwest::Error> for EmbeddingError {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() {
Self::Timeout(err.to_string())
} else if err.is_status() {
Self::HttpError(format!(
"HTTP {}: {err}",
err.status().map_or_else(|| "<unknown>".to_string(), |s| s.to_string())
))
} else {
Self::ApiError(err.to_string())
}
}
}
pub type Result<T> = std::result::Result<T, EmbeddingError>;