clickhouse_datafusion/utils/
errors.rs

1use clickhouse_arrow::Error as ClickhouseNativeError;
2use datafusion::error::DataFusionError;
3
4/// Helper to map [`ClickhouseNativeError`] to [`DataFusionError`]
5pub fn map_clickhouse_err(error: ClickhouseNativeError) -> DataFusionError {
6    DataFusionError::External(Box::new(error))
7}
8
9/// Helper to map any [`std::error::Error`] to [`DataFusionError`]
10pub fn map_external_err<E>(error: E) -> DataFusionError
11where
12    E: std::error::Error + Send + Sync + 'static,
13{
14    DataFusionError::External(Box::new(error))
15}
16
17#[cfg(test)]
18mod tests {
19    use std::fmt;
20
21    use clickhouse_arrow::Error as ClickhouseNativeError;
22
23    use super::*;
24
25    #[derive(Debug)]
26    struct TestError {
27        message: String,
28    }
29
30    impl fmt::Display for TestError {
31        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.message) }
32    }
33
34    impl std::error::Error for TestError {}
35
36    #[test]
37    fn test_map_clickhouse_err() {
38        let clickhouse_error = ClickhouseNativeError::Protocol("test error".to_string());
39        let datafusion_error = map_clickhouse_err(clickhouse_error);
40
41        match datafusion_error {
42            DataFusionError::External(boxed_error) => {
43                assert_eq!(boxed_error.to_string(), "protocol error: test error");
44            }
45            _ => panic!("Expected External error"),
46        }
47    }
48
49    #[test]
50    fn test_map_external_err() {
51        let test_error = TestError { message: "custom test error".to_string() };
52        let datafusion_error = map_external_err(test_error);
53
54        match datafusion_error {
55            DataFusionError::External(boxed_error) => {
56                assert_eq!(boxed_error.to_string(), "custom test error");
57            }
58            _ => panic!("Expected External error"),
59        }
60    }
61
62    #[test]
63    fn test_map_external_err_with_io_error() {
64        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
65        let datafusion_error = map_external_err(io_error);
66
67        match datafusion_error {
68            DataFusionError::External(boxed_error) => {
69                assert_eq!(boxed_error.to_string(), "file not found");
70            }
71            _ => panic!("Expected External error"),
72        }
73    }
74}