Skip to main content

arrow_flight/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::error::Error;
6
7use arrow_schema::ArrowError;
8
9/// Errors for the Apache Arrow Flight crate
10#[derive(Debug)]
11pub enum FlightError {
12    /// Underlying arrow error
13    Arrow(ArrowError),
14    /// Returned when functionality is not yet available.
15    NotYetImplemented(String),
16    /// Error from the underlying tonic library
17    Tonic(Box<tonic::Status>),
18    /// Some unexpected message was received
19    ProtocolError(String),
20    /// An error occurred during decoding
21    DecodeError(String),
22    /// External error that can provide source of error by calling `Error::source`.
23    ExternalError(Box<dyn Error + Send + Sync>),
24}
25
26impl FlightError {
27    /// Generate a new `FlightError::ProtocolError` variant.
28    pub fn protocol(message: impl Into<String>) -> Self {
29        Self::ProtocolError(message.into())
30    }
31
32    /// Wraps an external error in an `ArrowError`.
33    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
80// default conversion from FlightError to tonic treats everything
81// other than `Status` as an internal error
82impl 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
95/// Result type for the Apache Arrow Flight crate
96pub 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        // one level of wrapping
108        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        // flight error that wraps an arrow error that wraps a flight error
129        let e1 = FlightError::DecodeError("foo".into());
130        let e2 = ArrowError::ExternalError(Box::new(e1));
131        let e3 = FlightError::ExternalError(Box::new(e2));
132
133        // ensure we can find the lowest level error by following source()
134        let mut root_error: &dyn Error = &e3;
135        while let Some(source) = root_error.source() {
136            // walk the next level
137            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        // use Box in variants to keep this size down
147        assert_eq!(std::mem::size_of::<FlightError>(), 32);
148    }
149}