Skip to main content

mnem_embed_providers/
error.rs

1//! Error type for embedding-provider adapters.
2//!
3//! Deliberately coarse categories that a CLI can surface with concrete
4//! remediation (missing env var, network down, model not found, dim
5//! mismatch, ...). Avoids leaking internal HTTP library types.
6
7use thiserror::Error;
8
9/// Every fallible operation on an [`crate::Embedder`] returns this.
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum EmbedError {
13    /// Network / transport failure: TCP refused, DNS, TLS handshake,
14    /// read timeout.
15    #[error("network error: {0}")]
16    Network(String),
17
18    /// Provider rejected our credentials (HTTP 401 / 403).
19    #[error("provider authentication failed: {0}")]
20    Auth(String),
21
22    /// Provider rate-limited (HTTP 429). Fail-fast; a retry-with-backoff
23    /// layer belongs in the caller, not in the adapter.
24    #[error("provider rate-limited the request (HTTP 429): {0}")]
25    RateLimited(String),
26
27    /// Client-side 4xx other than 401/403/429 (bad model name, invalid
28    /// JSON, input too long, ...).
29    #[error("provider rejected the request (HTTP {status}): {body}")]
30    BadRequest {
31        /// HTTP status code.
32        status: u16,
33        /// Response body (usually a JSON-encoded error message).
34        body: String,
35    },
36
37    /// Provider-side 5xx. Treated as transient; caller decides retry.
38    #[error("provider returned 5xx (HTTP {status}): {body}")]
39    Server {
40        /// HTTP status code.
41        status: u16,
42        /// Response body.
43        body: String,
44    },
45
46    /// Response parsed as JSON but did not match the expected shape.
47    #[error("failed to decode provider response: {0}")]
48    Decode(String),
49
50    /// Provider returned a vector whose length disagrees with the
51    /// configured dimension.
52    #[error("dim mismatch: expected {expected}, got {got}")]
53    DimMismatch {
54        /// Dimension the adapter expected (from config or prior calls).
55        expected: u32,
56        /// Dimension the provider actually returned.
57        got: u32,
58    },
59
60    /// Config is malformed (e.g. unknown model string, invalid URL).
61    #[error("config error: {0}")]
62    Config(String),
63
64    /// The env var named by `api_key_env` is not set in the process
65    /// environment. Surfaced verbatim so the CLI can suggest `export`.
66    #[error("environment variable {var} is not set")]
67    MissingApiKey {
68        /// Name of the missing env var, exactly as configured.
69        var: String,
70    },
71}