Skip to main content

llm_embedding/
error.rs

1//! Error type for the embedding crate.
2
3use thiserror::Error;
4
5/// Embedding-specific error.
6#[derive(Debug, Error)]
7pub enum EmbeddingError {
8    /// Local configuration is invalid before any request is sent.
9    #[error("invalid embedding config: {0}")]
10    Config(String),
11
12    /// The request payload is invalid before it reaches the transport.
13    #[error("invalid embedding request: {0}")]
14    InvalidRequest(String),
15
16    /// The remote provider returned a non-success response.
17    #[error("embedding provider returned {status}: {message}")]
18    Provider {
19        /// HTTP status code reported by the provider.
20        status: u16,
21        /// Short error description from the provider response.
22        message: String,
23    },
24
25    /// The provider response could not be decoded.
26    #[error("failed to decode embedding response: {0}")]
27    Decode(String),
28
29    /// The HTTP client itself failed (network, timeout, TLS).
30    #[error("embedding transport failure: {0}")]
31    Transport(String),
32}
33
34/// Result alias for embedding operations.
35pub type Result<T> = std::result::Result<T, EmbeddingError>;
36
37impl From<reqwest::Error> for EmbeddingError {
38    fn from(err: reqwest::Error) -> Self {
39        if let Some(status) = err.status() {
40            EmbeddingError::Provider {
41                status: status.as_u16(),
42                message: err.to_string(),
43            }
44        } else {
45            EmbeddingError::Transport(err.to_string())
46        }
47    }
48}