pylate_rs/
error.rs

1use thiserror::Error;
2
3#[cfg(feature = "wasm")]
4use wasm_bindgen::prelude::*;
5
6/// Custom error enum for all possible errors in the ColBERT library.
7///
8/// This enum consolidates errors from various dependencies like `candle_core`,
9/// `tokenizers`, and `serde_json`, as well as custom operational errors.
10#[derive(Error, Debug)]
11pub enum ColbertError {
12    /// Error originating from the `candle_core` library.
13    #[error("Candle Error: {0}")]
14    Candle(#[from] candle_core::Error),
15
16    /// Error originating from the `tokenizers` library.
17    #[error("Tokenizer Error: {0}")]
18    Tokenizer(String),
19
20    /// Error related to JSON serialization or deserialization.
21    #[error("JSON Parsing Error: {0}")]
22    Json(#[from] serde_json::Error),
23
24    /// Error related to WASM-bindgen serialization or deserialization.
25    #[cfg(feature = "wasm")]
26    #[error("WASM Bindgen Deserialization Error: {0}")]
27    SerdeWasm(#[from] serde_wasm_bindgen::Error),
28
29    /// Custom operational errors, e.g., missing configuration values.
30    #[error("Operation Error: {0}")]
31    Operation(String),
32
33    /// Error originating from the `hf-hub` library for Hugging Face Hub interactions.
34    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
35    #[error("Hugging Face Hub Error: {0}")]
36    Hub(#[from] hf_hub::api::sync::ApiError),
37
38    /// I/O errors, typically from reading model files.
39    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
40    #[error("I/O Error: {0}")]
41    Io(#[from] std::io::Error),
42}
43
44impl From<Box<dyn std::error::Error + Send + Sync>> for ColbertError {
45    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
46        ColbertError::Tokenizer(err.to_string())
47    }
48}
49
50#[cfg(feature = "wasm")]
51impl From<ColbertError> for JsValue {
52    fn from(err: ColbertError) -> Self {
53        JsValue::from_str(&err.to_string())
54    }
55}