Skip to main content

docling_rag/
error.rs

1//! Error type for the RAG subsystem.
2
3use std::fmt;
4
5/// The crate-wide result alias.
6pub type Result<T> = std::result::Result<T, RagError>;
7
8/// Every fallible operation in the crate surfaces one of these.
9#[derive(Debug, thiserror::Error)]
10pub enum RagError {
11    /// Configuration was missing or invalid (bad env var, unknown backend name, …).
12    #[error("configuration error: {0}")]
13    Config(String),
14
15    /// A document could not be converted to Markdown by `docling.rs`.
16    #[error("document conversion failed: {0}")]
17    Conversion(String),
18
19    /// An embedding provider (Ollama / Gemini / ONNX / …) failed.
20    #[error("embedding error: {0}")]
21    Embedding(String),
22
23    /// The vector store / database failed.
24    #[error("store error: {0}")]
25    Store(String),
26
27    /// The chat/LLM provider (OpenRouter) failed.
28    #[error("llm error: {0}")]
29    Llm(String),
30
31    /// A document source (folder / FTP / SFTP) failed.
32    #[error("source error: {0}")]
33    Source(String),
34
35    /// A message queue backend failed.
36    #[error("queue error: {0}")]
37    Queue(String),
38
39    /// An HTTP request failed.
40    #[error("http error: {0}")]
41    Http(String),
42
43    /// I/O error.
44    #[error("io error: {0}")]
45    Io(#[from] std::io::Error),
46
47    /// JSON (de)serialization error.
48    #[error("json error: {0}")]
49    Json(#[from] serde_json::Error),
50
51    /// A feature-gated backend was requested but the crate was built without it.
52    #[error("backend '{0}' is not available: rebuild with the '{1}' cargo feature")]
53    FeatureDisabled(String, String),
54}
55
56impl RagError {
57    /// Helper to build a [`RagError::Config`] from anything printable.
58    pub fn config(msg: impl fmt::Display) -> Self {
59        RagError::Config(msg.to_string())
60    }
61}
62
63impl From<reqwest::Error> for RagError {
64    fn from(e: reqwest::Error) -> Self {
65        RagError::Http(e.to_string())
66    }
67}
68
69impl From<sqlx::Error> for RagError {
70    fn from(e: sqlx::Error) -> Self {
71        RagError::Store(e.to_string())
72    }
73}