Skip to main content

docbert_pylate/
error.rs

1use thiserror::Error;
2
3/// Custom error enum for all possible errors in the ColBERT library.
4///
5/// This enum consolidates errors from various dependencies like `candle_core`,
6/// `tokenizers`, and `serde_json`, as well as custom operational errors.
7#[derive(Error, Debug)]
8pub enum ColbertError {
9    /// Error originating from the `candle_core` library.
10    #[error("Candle Error: {0}")]
11    Candle(#[from] candle_core::Error),
12
13    /// Error originating from the `tokenizers` library.
14    #[error("Tokenizer Error: {0}")]
15    Tokenizer(String),
16
17    /// Error related to JSON serialization or deserialization.
18    #[error("JSON Parsing Error: {0}")]
19    Json(#[from] serde_json::Error),
20
21    /// Custom operational errors, e.g., missing configuration values.
22    #[error("Operation Error: {0}")]
23    Operation(String),
24
25    /// Error originating from the `hf-hub` library for Hugging Face Hub interactions.
26    #[error("Hugging Face Hub Error: {0}")]
27    Hub(#[from] hf_hub::api::sync::ApiError),
28
29    /// I/O errors, typically from reading model files.
30    #[error("I/O Error: {0}")]
31    Io(#[from] std::io::Error),
32}
33
34impl From<Box<dyn std::error::Error + Send + Sync>> for ColbertError {
35    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
36        ColbertError::Tokenizer(err.to_string())
37    }
38}
39
40impl From<ColbertError> for candle_core::Error {
41    fn from(err: ColbertError) -> Self {
42        match err {
43            // If the error is already a Candle error, we can just return it directly.
44            ColbertError::Candle(e) => e,
45            // For any other type of `ColbertError`, we convert it to a string
46            // and wrap it in the generic `candle_core::Error::Msg` variant.
47            _ => candle_core::Error::Msg(err.to_string()),
48        }
49    }
50}