1use std::error::Error;
6
7use arrow_schema::ArrowError;
8
9#[derive(Debug)]
11pub enum FlightError {
12 Arrow(ArrowError),
14 NotYetImplemented(String),
16 Tonic(Box<tonic::Status>),
18 ProtocolError(String),
20 DecodeError(String),
22 ExternalError(Box<dyn Error + Send + Sync>),
24}
25
26impl FlightError {
27 pub fn protocol(message: impl Into<String>) -> Self {
29 Self::ProtocolError(message.into())
30 }
31
32 pub fn from_external_error(error: Box<dyn Error + Send + Sync>) -> Self {
34 Self::ExternalError(error)
35 }
36}
37
38impl std::fmt::Display for FlightError {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 match self {
41 FlightError::Arrow(source) => write!(f, "Arrow error: {source}"),
42 FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {desc}"),
43 FlightError::Tonic(source) => write!(f, "Tonic error: {source}"),
44 FlightError::ProtocolError(desc) => write!(f, "Protocol error: {desc}"),
45 FlightError::DecodeError(desc) => write!(f, "Decode error: {desc}"),
46 FlightError::ExternalError(source) => write!(f, "External error: {source}"),
47 }
48 }
49}
50
51impl Error for FlightError {
52 fn source(&self) -> Option<&(dyn Error + 'static)> {
53 match self {
54 FlightError::Arrow(source) => Some(source),
55 FlightError::Tonic(source) => Some(source),
56 FlightError::ExternalError(source) => Some(source.as_ref()),
57 _ => None,
58 }
59 }
60}
61
62impl From<tonic::Status> for FlightError {
63 fn from(status: tonic::Status) -> Self {
64 Self::Tonic(Box::new(status))
65 }
66}
67
68impl From<prost::DecodeError> for FlightError {
69 fn from(error: prost::DecodeError) -> Self {
70 Self::DecodeError(error.to_string())
71 }
72}
73
74impl From<ArrowError> for FlightError {
75 fn from(value: ArrowError) -> Self {
76 Self::Arrow(value)
77 }
78}
79
80impl From<FlightError> for tonic::Status {
83 fn from(value: FlightError) -> Self {
84 match value {
85 FlightError::Arrow(e) => tonic::Status::internal(e.to_string()),
86 FlightError::NotYetImplemented(e) => tonic::Status::internal(e),
87 FlightError::Tonic(status) => *status,
88 FlightError::ProtocolError(e) => tonic::Status::internal(e),
89 FlightError::DecodeError(e) => tonic::Status::internal(e),
90 FlightError::ExternalError(e) => tonic::Status::internal(e.to_string()),
91 }
92 }
93}
94
95pub type Result<T> = std::result::Result<T, FlightError>;
97
98#[cfg(test)]
99mod test {
100 use super::*;
101
102 #[test]
103 fn error_source() {
104 let e1 = FlightError::DecodeError("foo".into());
105 assert!(e1.source().is_none());
106
107 let e2 = FlightError::ExternalError(Box::new(e1));
109 let source = e2.source().unwrap().downcast_ref::<FlightError>().unwrap();
110 assert!(matches!(source, FlightError::DecodeError(_)));
111
112 let e3 = FlightError::ExternalError(Box::new(e2));
113 let source = e3
114 .source()
115 .unwrap()
116 .downcast_ref::<FlightError>()
117 .unwrap()
118 .source()
119 .unwrap()
120 .downcast_ref::<FlightError>()
121 .unwrap();
122
123 assert!(matches!(source, FlightError::DecodeError(_)));
124 }
125
126 #[test]
127 fn error_through_arrow() {
128 let e1 = FlightError::DecodeError("foo".into());
130 let e2 = ArrowError::ExternalError(Box::new(e1));
131 let e3 = FlightError::ExternalError(Box::new(e2));
132
133 let mut root_error: &dyn Error = &e3;
135 while let Some(source) = root_error.source() {
136 root_error = source;
138 }
139
140 let source = root_error.downcast_ref::<FlightError>().unwrap();
141 assert!(matches!(source, FlightError::DecodeError(_)));
142 }
143
144 #[test]
145 fn test_error_size() {
146 assert_eq!(std::mem::size_of::<FlightError>(), 32);
148 }
149}