use std::error::Error;
use arrow_schema::ArrowError;
#[derive(Debug)]
pub enum FlightError {
Arrow(ArrowError),
NotYetImplemented(String),
Tonic(tonic::Status),
ProtocolError(String),
DecodeError(String),
ExternalError(Box<dyn Error + Send + Sync>),
}
impl FlightError {
pub fn protocol(message: impl Into<String>) -> Self {
Self::ProtocolError(message.into())
}
pub fn from_external_error(error: Box<dyn Error + Send + Sync>) -> Self {
Self::ExternalError(error)
}
}
impl std::fmt::Display for FlightError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FlightError::Arrow(source) => write!(f, "Arrow error: {}", source),
FlightError::NotYetImplemented(desc) => write!(f, "Not yet implemented: {}", desc),
FlightError::Tonic(source) => write!(f, "Tonic error: {}", source),
FlightError::ProtocolError(desc) => write!(f, "Protocol error: {}", desc),
FlightError::DecodeError(desc) => write!(f, "Decode error: {}", desc),
FlightError::ExternalError(source) => write!(f, "External error: {}", source),
}
}
}
impl Error for FlightError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
FlightError::Arrow(source) => Some(source),
FlightError::Tonic(source) => Some(source),
FlightError::ExternalError(source) => Some(source.as_ref()),
_ => None,
}
}
}
impl From<tonic::Status> for FlightError {
fn from(status: tonic::Status) -> Self {
Self::Tonic(status)
}
}
impl From<ArrowError> for FlightError {
fn from(value: ArrowError) -> Self {
Self::Arrow(value)
}
}
impl From<FlightError> for tonic::Status {
fn from(value: FlightError) -> Self {
match value {
FlightError::Arrow(e) => tonic::Status::internal(e.to_string()),
FlightError::NotYetImplemented(e) => tonic::Status::internal(e),
FlightError::Tonic(status) => status,
FlightError::ProtocolError(e) => tonic::Status::internal(e),
FlightError::DecodeError(e) => tonic::Status::internal(e),
FlightError::ExternalError(e) => tonic::Status::internal(e.to_string()),
}
}
}
pub type Result<T> = std::result::Result<T, FlightError>;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn error_source() {
let e1 = FlightError::DecodeError("foo".into());
assert!(e1.source().is_none());
let e2 = FlightError::ExternalError(Box::new(e1));
let source = e2.source().unwrap().downcast_ref::<FlightError>().unwrap();
assert!(matches!(source, FlightError::DecodeError(_)));
let e3 = FlightError::ExternalError(Box::new(e2));
let source = e3
.source()
.unwrap()
.downcast_ref::<FlightError>()
.unwrap()
.source()
.unwrap()
.downcast_ref::<FlightError>()
.unwrap();
assert!(matches!(source, FlightError::DecodeError(_)));
}
#[test]
fn error_through_arrow() {
let e1 = FlightError::DecodeError("foo".into());
let e2 = ArrowError::ExternalError(Box::new(e1));
let e3 = FlightError::ExternalError(Box::new(e2));
let mut root_error: &dyn Error = &e3;
while let Some(source) = root_error.source() {
root_error = source;
}
let source = root_error.downcast_ref::<FlightError>().unwrap();
assert!(matches!(source, FlightError::DecodeError(_)));
}
}