lumosai_vector_fastembed/
error.rs

1//! Error types for FastEmbed integration
2
3use thiserror::Error;
4
5/// Result type for FastEmbed operations
6pub type Result<T> = std::result::Result<T, FastEmbedError>;
7
8/// Errors that can occur when using FastEmbed
9#[derive(Error, Debug)]
10pub enum FastEmbedError {
11    /// Model initialization failed
12    #[error("Model initialization failed: {0}")]
13    ModelInitialization(String),
14    
15    /// Model not initialized
16    #[error("Model not initialized: {0}")]
17    ModelNotInitialized(String),
18    
19    /// Embedding generation failed
20    #[error("Embedding generation failed: {0}")]
21    EmbeddingGeneration(String),
22    
23    /// Text is too long for the model
24    #[error("Text too long: {length} characters, maximum: {max_length}")]
25    TextTooLong { length: usize, max_length: usize },
26    
27    /// Invalid model configuration
28    #[error("Invalid model configuration: {0}")]
29    InvalidConfiguration(String),
30    
31    /// IO error (file operations, cache, etc.)
32    #[error("IO error: {0}")]
33    Io(#[from] std::io::Error),
34    
35    /// Serialization/deserialization error
36    #[error("Serialization error: {0}")]
37    Serialization(#[from] serde_json::Error),
38    
39    /// Model download failed
40    #[error("Model download failed: {0}")]
41    ModelDownload(String),
42    
43    /// Cache directory error
44    #[error("Cache directory error: {0}")]
45    CacheDirectory(String),
46    
47    /// Unsupported operation
48    #[error("Unsupported operation: {0}")]
49    UnsupportedOperation(String),
50    
51    /// Generic error
52    #[error("FastEmbed error: {0}")]
53    Generic(String),
54}
55
56impl FastEmbedError {
57    /// Create a new model initialization error
58    pub fn model_init<S: Into<String>>(msg: S) -> Self {
59        Self::ModelInitialization(msg.into())
60    }
61    
62    /// Create a new embedding generation error
63    pub fn embedding<S: Into<String>>(msg: S) -> Self {
64        Self::EmbeddingGeneration(msg.into())
65    }
66    
67    /// Create a new configuration error
68    pub fn config<S: Into<String>>(msg: S) -> Self {
69        Self::InvalidConfiguration(msg.into())
70    }
71    
72    /// Create a new generic error
73    pub fn generic<S: Into<String>>(msg: S) -> Self {
74        Self::Generic(msg.into())
75    }
76    
77    /// Check if this is a recoverable error
78    pub fn is_recoverable(&self) -> bool {
79        match self {
80            FastEmbedError::ModelInitialization(_) => false,
81            FastEmbedError::ModelNotInitialized(_) => true,
82            FastEmbedError::EmbeddingGeneration(_) => true,
83            FastEmbedError::TextTooLong { .. } => true,
84            FastEmbedError::InvalidConfiguration(_) => false,
85            FastEmbedError::Io(_) => true,
86            FastEmbedError::Serialization(_) => false,
87            FastEmbedError::ModelDownload(_) => true,
88            FastEmbedError::CacheDirectory(_) => true,
89            FastEmbedError::UnsupportedOperation(_) => false,
90            FastEmbedError::Generic(_) => true,
91        }
92    }
93    
94    /// Get error category for logging/monitoring
95    pub fn category(&self) -> &'static str {
96        match self {
97            FastEmbedError::ModelInitialization(_) => "model_init",
98            FastEmbedError::ModelNotInitialized(_) => "model_not_init",
99            FastEmbedError::EmbeddingGeneration(_) => "embedding",
100            FastEmbedError::TextTooLong { .. } => "text_length",
101            FastEmbedError::InvalidConfiguration(_) => "config",
102            FastEmbedError::Io(_) => "io",
103            FastEmbedError::Serialization(_) => "serialization",
104            FastEmbedError::ModelDownload(_) => "download",
105            FastEmbedError::CacheDirectory(_) => "cache",
106            FastEmbedError::UnsupportedOperation(_) => "unsupported",
107            FastEmbedError::Generic(_) => "generic",
108        }
109    }
110}
111
112// Conversion to lumosai_vector_core::error::VectorError
113impl From<FastEmbedError> for lumosai_vector_core::error::VectorError {
114    fn from(err: FastEmbedError) -> Self {
115        lumosai_vector_core::error::VectorError::EmbeddingError(err.to_string())
116    }
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122    
123    #[test]
124    fn test_error_creation() {
125        let err = FastEmbedError::model_init("Test error");
126        assert!(matches!(err, FastEmbedError::ModelInitialization(_)));
127        assert!(!err.is_recoverable());
128        assert_eq!(err.category(), "model_init");
129    }
130    
131    #[test]
132    fn test_text_too_long_error() {
133        let err = FastEmbedError::TextTooLong {
134            length: 1000,
135            max_length: 512,
136        };
137        assert!(err.is_recoverable());
138        assert_eq!(err.category(), "text_length");
139    }
140    
141    #[test]
142    fn test_error_conversion() {
143        let fastembed_err = FastEmbedError::embedding("Test embedding error");
144        let vector_err: lumosai_vector_core::error::VectorError = fastembed_err.into();
145        
146        match vector_err {
147            lumosai_vector_core::error::VectorError::EmbeddingError(msg) => {
148                assert!(msg.contains("Test embedding error"));
149            }
150            _ => panic!("Expected EmbeddingError"),
151        }
152    }
153}