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