use std::fmt::{Debug, Display, Formatter};
use std::io::Write;
use std::error::Error;
#[derive(Debug)]
pub enum ArrowError {
NotYetImplemented(String),
ExternalError(Box<dyn Error + Send + Sync>),
CastError(String),
MemoryError(String),
ParseError(String),
SchemaError(String),
ComputeError(String),
DivideByZero,
CsvError(String),
JsonError(String),
IoError(String),
InvalidArgumentError(String),
ParquetError(String),
CDataInterface(String),
DictionaryKeyOverflowError,
RunEndIndexOverflowError,
}
impl ArrowError {
pub fn from_external_error(error: Box<dyn Error + Send + Sync>) -> Self {
Self::ExternalError(error)
}
}
impl From<std::io::Error> for ArrowError {
fn from(error: std::io::Error) -> Self {
ArrowError::IoError(error.to_string())
}
}
impl From<std::string::FromUtf8Error> for ArrowError {
fn from(error: std::string::FromUtf8Error) -> Self {
ArrowError::ParseError(error.to_string())
}
}
impl<W: Write> From<std::io::IntoInnerError<W>> for ArrowError {
fn from(error: std::io::IntoInnerError<W>) -> Self {
ArrowError::IoError(error.to_string())
}
}
impl Display for ArrowError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ArrowError::NotYetImplemented(source) => {
write!(f, "Not yet implemented: {}", &source)
}
ArrowError::ExternalError(source) => write!(f, "External error: {}", &source),
ArrowError::CastError(desc) => write!(f, "Cast error: {desc}"),
ArrowError::MemoryError(desc) => write!(f, "Memory error: {desc}"),
ArrowError::ParseError(desc) => write!(f, "Parser error: {desc}"),
ArrowError::SchemaError(desc) => write!(f, "Schema error: {desc}"),
ArrowError::ComputeError(desc) => write!(f, "Compute error: {desc}"),
ArrowError::DivideByZero => write!(f, "Divide by zero error"),
ArrowError::CsvError(desc) => write!(f, "Csv error: {desc}"),
ArrowError::JsonError(desc) => write!(f, "Json error: {desc}"),
ArrowError::IoError(desc) => write!(f, "Io error: {desc}"),
ArrowError::InvalidArgumentError(desc) => {
write!(f, "Invalid argument error: {desc}")
}
ArrowError::ParquetError(desc) => {
write!(f, "Parquet argument error: {desc}")
}
ArrowError::CDataInterface(desc) => {
write!(f, "C Data interface error: {desc}")
}
ArrowError::DictionaryKeyOverflowError => {
write!(f, "Dictionary key bigger than the key type")
}
ArrowError::RunEndIndexOverflowError => {
write!(f, "Run end encoded array index overflow error")
}
}
}
}
impl Error for ArrowError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
if let Self::ExternalError(e) = self {
Some(e.as_ref())
} else {
None
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn error_source() {
let e1 = ArrowError::DivideByZero;
assert!(e1.source().is_none());
let e2 = ArrowError::ExternalError(Box::new(e1));
let source = e2.source().unwrap().downcast_ref::<ArrowError>().unwrap();
assert!(matches!(source, ArrowError::DivideByZero));
let e3 = ArrowError::ExternalError(Box::new(e2));
let source = e3
.source()
.unwrap()
.downcast_ref::<ArrowError>()
.unwrap()
.source()
.unwrap()
.downcast_ref::<ArrowError>()
.unwrap();
assert!(matches!(source, ArrowError::DivideByZero));
}
}