Skip to main content

llm_kernel/
error.rs

1//! Error types for llm-kernel.
2
3use thiserror::Error;
4
5/// Errors that can occur when using llm-kernel.
6///
7/// `#[non_exhaustive]`: new variants may be added in any minor release. External
8/// `match`es must include a `_ =>` arm; prefer matching only the variants you
9/// act on (e.g. [`KernelError::RateLimited`] / [`KernelError::Http`] for retry).
10#[non_exhaustive]
11#[derive(Debug, Error)]
12pub enum KernelError {
13    /// An LLM API returned an error response.
14    #[error("LLM API error: {0}")]
15    LlmApi(String),
16
17    /// The LLM API rate-limited the request.
18    #[error("LLM rate limited: retry after {0}s")]
19    RateLimited(u64),
20
21    /// An HTTP error occurred (non-200 status with code).
22    #[error("HTTP {status}: {message}")]
23    Http {
24        /// HTTP status code.
25        status: u16,
26        /// Error message from the response body.
27        message: String,
28    },
29
30    /// A request timed out.
31    #[error("Request timed out after {0}s")]
32    Timeout(u64),
33
34    /// A configuration error (missing field, bad format, etc.).
35    #[error("Config error: {0}")]
36    Config(String),
37
38    /// A store (SQLite) error.
39    #[error("Store error: {0}")]
40    Store(String),
41
42    /// A secrets vault error.
43    #[error("Vault error: {0}")]
44    Vault(String),
45
46    /// An I/O error.
47    #[error("IO error: {0}")]
48    Io(#[from] std::io::Error),
49
50    /// A search backend error.
51    #[error("Search error: {0}")]
52    Search(String),
53
54    /// An embedding provider or vector-index error.
55    #[error("Embedding error: {0}")]
56    Embedding(String),
57
58    /// A model-discovery error.
59    #[error("Discovery error: {0}")]
60    Discovery(String),
61
62    /// A serialization/deserialization error.
63    ///
64    /// Available whenever any feature that pulls in `serde_json` is enabled —
65    /// not just `provider`. Add new serde_json-using features to this `any(...)`
66    /// list so a `?`-conversion into `KernelError` keeps compiling everywhere
67    /// `serde_json` is available.
68    #[cfg(any(
69        feature = "provider",
70        feature = "client-async",
71        feature = "graph",
72        feature = "mcp",
73        feature = "install",
74        feature = "search",
75        feature = "vector-index",
76        feature = "qdrant",
77        feature = "elastic",
78        feature = "embedding-openai",
79        feature = "telemetry"
80    ))]
81    #[error("Serialization error: {0}")]
82    Serialization(#[from] serde_json::Error),
83}
84
85impl KernelError {
86    /// Construct an [`KernelError::Embedding`] from any displayable error.
87    ///
88    /// Convenience for mapping external errors (HTTP clients, ONNX runtimes) at
89    /// `?` sites: `.map_err(KernelError::embedding)?`.
90    pub fn embedding(e: impl std::fmt::Display) -> Self {
91        Self::Embedding(e.to_string())
92    }
93
94    /// Construct an [`KernelError::Discovery`] from any displayable error.
95    pub fn discovery(e: impl std::fmt::Display) -> Self {
96        Self::Discovery(e.to_string())
97    }
98}
99
100/// Alias for `Result<T, KernelError>`.
101pub type Result<T> = std::result::Result<T, KernelError>;