Skip to main content

cel_memory_sqlite/
error.rs

1//! Storage-side error type. Converted to [`MemoryError`] on the way out
2//! of the [`SqliteMemoryProvider`](crate::SqliteMemoryProvider).
3//!
4//! [`MemoryError`]: cel_memory::MemoryError
5
6use cel_memory::MemoryError;
7use thiserror::Error;
8
9/// Errors that originate inside the SQLite memory backend.
10#[derive(Debug, Error)]
11pub enum SqliteMemoryError {
12    /// rusqlite returned an error.
13    #[error("sqlite error: {0}")]
14    Sqlite(#[from] rusqlite::Error),
15
16    /// Migration file failed to apply.
17    #[error("migration {name} failed: {source}")]
18    Migration {
19        /// Migration file basename, e.g. `"001_initial.sql"`.
20        name: String,
21        /// Underlying SQLite error.
22        #[source]
23        source: rusqlite::Error,
24    },
25
26    /// sqlite-vec couldn't be loaded into the connection.
27    #[error("sqlite-vec load failed: {0}")]
28    VecLoad(String),
29
30    /// `tokio::task::spawn_blocking` panicked.
31    #[error("blocking task panicked: {0}")]
32    BlockingJoin(String),
33
34    /// Embedder produced a vector with the wrong dimension.
35    #[error("embedding dim mismatch: provider expects {expected}, embedder produced {actual}")]
36    DimMismatch {
37        /// Dim the schema expects (matches `memory_vec`'s declared dim).
38        expected: usize,
39        /// Dim the embedder actually produced.
40        actual: usize,
41    },
42
43    /// JSON serialization or deserialization failed inside the storage layer.
44    #[error("json error: {0}")]
45    Json(#[from] serde_json::Error),
46}
47
48impl From<SqliteMemoryError> for MemoryError {
49    fn from(e: SqliteMemoryError) -> Self {
50        match e {
51            SqliteMemoryError::DimMismatch { expected, actual } => MemoryError::InvalidArgument(
52                format!("embedding dim mismatch: expected {expected}, got {actual}"),
53            ),
54            SqliteMemoryError::Json(err) => MemoryError::Storage(format!("json: {err}")),
55            other => MemoryError::Storage(other.to_string()),
56        }
57    }
58}