Skip to content

Commit

Permalink
use thiserror for error (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Sep 25, 2024
1 parent 869998e commit 6798e60
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions pyo3-arrow/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
use pyo3::exceptions::{PyException, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::PyDowncastError;
use thiserror::Error;

/// The Error variants returned by this crate.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum PyArrowError {
/// A wrapped [arrow::error::ArrowError]
ArrowError(arrow::error::ArrowError),
#[error(transparent)]
ArrowError(#[from] arrow::error::ArrowError),

/// A wrapped [PyErr]
PyErr(PyErr),
#[error(transparent)]
PyErr(#[from] PyErr),
}

impl From<PyArrowError> for PyErr {
fn from(error: PyArrowError) -> Self {
match error {
PyArrowError::ArrowError(err) => PyException::new_err(err.to_string()),
PyArrowError::PyErr(err) => err,
PyArrowError::ArrowError(err) => PyException::new_err(err.to_string()),
}
}
}

impl From<arrow::error::ArrowError> for PyArrowError {
fn from(other: arrow::error::ArrowError) -> Self {
Self::ArrowError(other)
}
}

impl From<PyTypeError> for PyArrowError {
fn from(other: PyTypeError) -> Self {
Self::PyErr((&other).into())
Expand All @@ -48,11 +48,5 @@ impl From<PyValueError> for PyArrowError {
}
}

impl From<PyErr> for PyArrowError {
fn from(other: PyErr) -> Self {
Self::PyErr(other)
}
}

/// A type wrapper around `Result<T, PyArrowError>`.
pub type PyArrowResult<T> = Result<T, PyArrowError>;

0 comments on commit 6798e60

Please sign in to comment.