lumosai_vector_fastembed/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, FastEmbedError>;
7
8#[derive(Error, Debug)]
10pub enum FastEmbedError {
11 #[error("Model initialization failed: {0}")]
13 ModelInitialization(String),
14
15 #[error("Model not initialized: {0}")]
17 ModelNotInitialized(String),
18
19 #[error("Embedding generation failed: {0}")]
21 EmbeddingGeneration(String),
22
23 #[error("Text too long: {length} characters, maximum: {max_length}")]
25 TextTooLong { length: usize, max_length: usize },
26
27 #[error("Invalid model configuration: {0}")]
29 InvalidConfiguration(String),
30
31 #[error("IO error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("Serialization error: {0}")]
37 Serialization(#[from] serde_json::Error),
38
39 #[error("Model download failed: {0}")]
41 ModelDownload(String),
42
43 #[error("Cache directory error: {0}")]
45 CacheDirectory(String),
46
47 #[error("Unsupported operation: {0}")]
49 UnsupportedOperation(String),
50
51 #[error("FastEmbed error: {0}")]
53 Generic(String),
54}
55
56impl FastEmbedError {
57 pub fn model_init<S: Into<String>>(msg: S) -> Self {
59 Self::ModelInitialization(msg.into())
60 }
61
62 pub fn embedding<S: Into<String>>(msg: S) -> Self {
64 Self::EmbeddingGeneration(msg.into())
65 }
66
67 pub fn config<S: Into<String>>(msg: S) -> Self {
69 Self::InvalidConfiguration(msg.into())
70 }
71
72 pub fn generic<S: Into<String>>(msg: S) -> Self {
74 Self::Generic(msg.into())
75 }
76
77 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 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
112impl 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}