Skip to main content

xml2arrow/
errors.rs

1use std::str::Utf8Error;
2
3#[cfg(feature = "python")]
4use arrow::pyarrow::PyArrowException;
5use derive_more::From;
6#[cfg(feature = "python")]
7use pyo3::PyErr;
8#[cfg(feature = "python")]
9use pyo3::create_exception;
10
11pub type Result<T> = core::result::Result<T, Error>;
12
13#[derive(Debug, From)]
14pub enum Error {
15    /// Errors from the QuickXML crate during XML parsing
16    #[from]
17    XmlParsing(quick_xml::Error),
18    /// Errors from the QuickXML crate that can be raised during parsing attributes.
19    #[from]
20    XmlParseAttr(quick_xml::events::attributes::AttrError),
21    // Errors from the QuickXML crate that can be raised when decoding or encoding.
22    #[from]
23    XmlParseEncoding(quick_xml::encoding::EncodingError),
24    /// Errors from the Serde YAML crate during configuration parsing
25    #[from]
26    Yaml(serde_yaml::Error),
27    /// Standard I/O errors
28    #[from]
29    Io(std::io::Error),
30    /// Errors from the arrow crate during Arrow operations
31    #[from]
32    Arrow(arrow::error::ArrowError),
33    /// Errors during UTF-8 string conversion
34    #[from]
35    Utf8Error(Utf8Error),
36    /// Custom error for unsupported data types, holding a descriptive message
37    UnsupportedDataType(String),
38    /// Errors that occur during parsing of values from strings to specific data types.
39    ParseError(String),
40    /// Error indicating that a table specified in the configuration was not found in the XML data.
41    /// Contains the XML path of the missing table.
42    TableNotFound(String),
43    /// Error indicating that there is no table on the stack.
44    NoTableOnStack,
45    /// Error when applying a scaling or an offset is attempted on unsupported data types.
46    UnsupportedConversion(String),
47}
48
49#[cfg(feature = "python")]
50create_exception!(
51    xml2arrow,
52    Xml2ArrowError,
53    pyo3::exceptions::PyException,
54    "Base exception for the xml2arrow package."
55);
56
57#[cfg(feature = "python")]
58create_exception!(
59    xml2arrow,
60    XmlParsingError,
61    Xml2ArrowError,
62    "Raised when an error occurs during XML parsing."
63);
64
65#[cfg(feature = "python")]
66create_exception!(
67    xml2arrow,
68    YamlParsingError,
69    Xml2ArrowError,
70    "Raised when an error occurs during YAML configuration parsing."
71);
72
73#[cfg(feature = "python")]
74create_exception!(
75    xml2arrow,
76    UnsupportedDataTypeError,
77    Xml2ArrowError,
78    "Raised when an unsupported data type is encountered."
79);
80
81#[cfg(feature = "python")]
82create_exception!(
83    xml2arrow,
84    TableNotFoundError,
85    Xml2ArrowError,
86    "Raised when a table specified in the configuration is not found in the XML data."
87);
88
89#[cfg(feature = "python")]
90create_exception!(
91    xml2arrow,
92    NoTableOnStackError,
93    Xml2ArrowError,
94    "Raised when an operation is performed that requires a table to be on the stack, but none is present."
95);
96
97#[cfg(feature = "python")]
98create_exception!(
99    xml2arrow,
100    ParseError,
101    Xml2ArrowError,
102    "Raised when an error occurs during parsing of values from strings to specific data types."
103);
104
105#[cfg(feature = "python")]
106create_exception!(
107    xml2arrow,
108    UnsupportedConversionError,
109    Xml2ArrowError,
110    "Raised when an unsupported conversion (scale or offset) is attempted."
111);
112
113#[cfg(feature = "python")]
114impl From<Error> for PyErr {
115    fn from(value: Error) -> Self {
116        match value {
117            Error::Io(e) => e.into(),
118            Error::Utf8Error(e) => e.into(),
119            Error::Arrow(e) => PyArrowException::new_err(e.to_string()),
120            Error::XmlParsing(e) => XmlParsingError::new_err(e.to_string()),
121            Error::XmlParseAttr(e) => XmlParsingError::new_err(e.to_string()),
122            Error::XmlParseEncoding(e) => XmlParsingError::new_err(e.to_string()),
123            Error::Yaml(e) => YamlParsingError::new_err(e.to_string()),
124            Error::UnsupportedDataType(e) => UnsupportedDataTypeError::new_err(e.to_string()),
125            Error::TableNotFound(e) => TableNotFoundError::new_err(e.to_string()),
126            Error::NoTableOnStack => {
127                NoTableOnStackError::new_err("There is no table on the stack".to_string())
128            }
129            Error::ParseError(e) => ParseError::new_err(e.to_string()),
130            Error::UnsupportedConversion(e) => UnsupportedConversionError::new_err(e.to_string()),
131        }
132    }
133}