use arrow_schema::ArrowError;
#[derive(Debug)]
pub enum FlightError {
Arrow(ArrowError),
NotYetImplemented(String),
Tonic(tonic::Status),
ProtocolError(String),
DecodeError(String),
ExternalError(Box<dyn std::error::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 std::error::Error + Send + Sync>) -> Self {
Self::ExternalError(error)
}
}
impl std::fmt::Display for FlightError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::error::Error for FlightError {}
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>;