datafusion_delta_sharing/
error.rs

1//! Delta Sharing errors types
2
3use std::{
4    error::Error,
5    fmt::{Display, Formatter},
6};
7
8use arrow_schema::ArrowError;
9use datafusion::error::DataFusionError;
10
11/// Error type for Delta Sharing.
12#[derive(Debug, Clone)]
13pub struct DeltaSharingError {
14    kind: DeltaSharingErrorKind,
15    message: String,
16}
17
18impl DeltaSharingError {
19    fn new(kind: DeltaSharingErrorKind, message: impl Into<String>) -> Self {
20        Self {
21            kind,
22            message: message.into(),
23        }
24    }
25
26    /// Retrieve the kind of the error
27    pub fn kind(&self) -> DeltaSharingErrorKind {
28        self.kind
29    }
30
31    /// Retrieve the message of the error
32    pub fn message(&self) -> &str {
33        self.message.as_ref()
34    }
35
36    /// Create a new profile error with a message
37    pub fn profile(message: impl Into<String>) -> Self {
38        Self::new(DeltaSharingErrorKind::ProfileError, message)
39    }
40
41    /// Create a new sharing client error with a message
42    pub fn client(message: impl Into<String>) -> Self {
43        Self::new(DeltaSharingErrorKind::ClientError, message)
44    }
45
46    /// Create a new sharing server error with a message
47    pub fn server(message: impl Into<String>) -> Self {
48        Self::new(DeltaSharingErrorKind::ServerError, message)
49    }
50
51    /// Create a new parse response error with a message
52    pub fn parse_response(message: impl Into<String>) -> Self {
53        Self::new(DeltaSharingErrorKind::ParseResponseError, message)
54    }
55
56    /// Create a new parse securable error with a message
57    pub fn parse_securable(message: impl Into<String>) -> Self {
58        Self::new(DeltaSharingErrorKind::ParseSecurableError, message)
59    }
60
61    /// Create a new request error with a message
62    pub fn request(message: impl Into<String>) -> Self {
63        Self::new(DeltaSharingErrorKind::RequestError, message)
64    }
65
66    /// Create a new other error with a message
67    pub fn other(message: impl Into<String>) -> Self {
68        Self::new(DeltaSharingErrorKind::Other, message)
69    }
70}
71
72/// Kind of Delta Sharing error
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
74pub enum DeltaSharingErrorKind {
75    /// Error related to Delta Sharing profile
76    ProfileError,
77    /// Error related to parsing shared object names
78    ParseSecurableError,
79    /// Error related to parsing the response from the server
80    ParseResponseError,
81    /// Error related to the the Delta Sharing Client
82    ClientError,
83    /// Error related to the Delta Sharing Server
84    ServerError,
85    /// Error related to the request
86    RequestError,
87    /// Other error
88    Other,
89}
90
91impl Display for DeltaSharingError {
92    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
93        write!(f, "[{}] {}", self.kind, self.message)
94    }
95}
96
97impl Display for DeltaSharingErrorKind {
98    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
99        match self {
100            Self::ProfileError => write!(f, "PROFILE_ERROR"),
101            Self::ParseSecurableError => write!(f, "ParseSecurableError"),
102            Self::ParseResponseError => write!(f, "ParseResponseError"),
103            Self::RequestError => write!(f, "RequestError"),
104            Self::ClientError => write!(f, "SHARING_CLIENT_ERROR"),
105            Self::ServerError => write!(f, "ServerError"),
106            Self::Other => write!(f, "Other"),
107        }
108    }
109}
110
111impl Error for DeltaSharingError {}
112
113impl From<reqwest::Error> for DeltaSharingError {
114    fn from(value: reqwest::Error) -> Self {
115        if value.is_decode() {
116            return Self::parse_response(value.to_string());
117        }
118        Self::request(value.to_string())
119    }
120}
121
122impl From<ArrowError> for DeltaSharingError {
123    fn from(value: ArrowError) -> Self {
124        Self::other(value.to_string())
125    }
126}
127
128impl From<DeltaSharingError> for DataFusionError {
129    fn from(e: DeltaSharingError) -> Self {
130        DataFusionError::Execution(e.to_string())
131    }
132}