Skip to main content

cvxtract/core/extractor/
error.rs

1use crate::core::loaders::LoaderError;
2
3/// Errors that can occur during structured extraction.
4#[derive(Debug)]
5pub enum ExtractionError {
6    /// The document could not be read or parsed (PDF, DOCX, etc.)
7    LoadError(LoaderError),
8    /// The LLM returned an error or an empty response.
9    ModelError(String),
10    /// The LLM output could not be parsed as JSON or deserialised into the target type.
11    ParseError(serde_json::Error),
12}
13
14impl std::fmt::Display for ExtractionError {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            ExtractionError::LoadError(e) => write!(f, "Document load error: {e}"),
18            ExtractionError::ModelError(msg) => write!(f, "Model error: {msg}"),
19            ExtractionError::ParseError(e) => write!(f, "JSON parse error: {e}"),
20        }
21    }
22}
23
24impl std::error::Error for ExtractionError {
25    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
26        match self {
27            ExtractionError::LoadError(e) => Some(e),
28            ExtractionError::ParseError(e) => Some(e),
29            ExtractionError::ModelError(_) => None,
30        }
31    }
32}
33
34impl From<LoaderError> for ExtractionError {
35    fn from(e: LoaderError) -> Self {
36        ExtractionError::LoadError(e)
37    }
38}
39
40impl From<serde_json::Error> for ExtractionError {
41    fn from(e: serde_json::Error) -> Self {
42        ExtractionError::ParseError(e)
43    }
44}