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#[derive(Debug, Error)]
7pub enum KernelError {
8    /// An LLM API returned an error response.
9    #[error("LLM API error: {0}")]
10    LlmApi(String),
11
12    /// The LLM API rate-limited the request.
13    #[error("LLM rate limited: retry after {0}s")]
14    RateLimited(u64),
15
16    /// An HTTP error occurred (non-200 status with code).
17    #[error("HTTP {status}: {message}")]
18    Http {
19        /// HTTP status code.
20        status: u16,
21        /// Error message from the response body.
22        message: String,
23    },
24
25    /// A request timed out.
26    #[error("Request timed out after {0}s")]
27    Timeout(u64),
28
29    /// A configuration error (missing field, bad format, etc.).
30    #[error("Config error: {0}")]
31    Config(String),
32
33    /// A store (SQLite) error.
34    #[error("Store error: {0}")]
35    Store(String),
36
37    /// A secrets vault error.
38    #[error("Vault error: {0}")]
39    Vault(String),
40
41    /// An I/O error.
42    #[error("IO error: {0}")]
43    Io(#[from] std::io::Error),
44
45    /// A search backend error.
46    #[error("Search error: {0}")]
47    Search(String),
48
49    /// An embedding provider or vector-index error.
50    #[error("Embedding error: {0}")]
51    Embedding(String),
52
53    /// A model-discovery error.
54    #[error("Discovery error: {0}")]
55    Discovery(String),
56
57    /// A serialization/deserialization error.
58    #[cfg(feature = "provider")]
59    #[error("Serialization error: {0}")]
60    Serialization(#[from] serde_json::Error),
61}
62
63impl KernelError {
64    /// Construct an [`KernelError::Embedding`] from any displayable error.
65    ///
66    /// Convenience for mapping external errors (HTTP clients, ONNX runtimes) at
67    /// `?` sites: `.map_err(KernelError::embedding)?`.
68    pub fn embedding(e: impl std::fmt::Display) -> Self {
69        Self::Embedding(e.to_string())
70    }
71
72    /// Construct an [`KernelError::Discovery`] from any displayable error.
73    pub fn discovery(e: impl std::fmt::Display) -> Self {
74        Self::Discovery(e.to_string())
75    }
76}
77
78/// Alias for `Result<T, KernelError>`.
79pub type Result<T> = std::result::Result<T, KernelError>;