datafusion_delta_sharing/
error.rs1use std::{
4 error::Error,
5 fmt::{Display, Formatter},
6};
7
8use arrow_schema::ArrowError;
9use datafusion::error::DataFusionError;
10
11#[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 pub fn kind(&self) -> DeltaSharingErrorKind {
28 self.kind
29 }
30
31 pub fn message(&self) -> &str {
33 self.message.as_ref()
34 }
35
36 pub fn profile(message: impl Into<String>) -> Self {
38 Self::new(DeltaSharingErrorKind::ProfileError, message)
39 }
40
41 pub fn client(message: impl Into<String>) -> Self {
43 Self::new(DeltaSharingErrorKind::ClientError, message)
44 }
45
46 pub fn server(message: impl Into<String>) -> Self {
48 Self::new(DeltaSharingErrorKind::ServerError, message)
49 }
50
51 pub fn parse_response(message: impl Into<String>) -> Self {
53 Self::new(DeltaSharingErrorKind::ParseResponseError, message)
54 }
55
56 pub fn parse_securable(message: impl Into<String>) -> Self {
58 Self::new(DeltaSharingErrorKind::ParseSecurableError, message)
59 }
60
61 pub fn request(message: impl Into<String>) -> Self {
63 Self::new(DeltaSharingErrorKind::RequestError, message)
64 }
65
66 pub fn other(message: impl Into<String>) -> Self {
68 Self::new(DeltaSharingErrorKind::Other, message)
69 }
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
74pub enum DeltaSharingErrorKind {
75 ProfileError,
77 ParseSecurableError,
79 ParseResponseError,
81 ClientError,
83 ServerError,
85 RequestError,
87 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}