minarrow_pyo3/error.rs
1// Copyright 2025 Peter Garfield Bower
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! # Error Module for minarrow-pyo3
16//!
17//! Provides error types and conversions between MinArrow errors and Python exceptions.
18
19use pyo3::exceptions::{PyRuntimeError, PyTypeError, PyValueError};
20use pyo3::prelude::*;
21use thiserror::Error;
22
23/// Error type for minarrow-pyo3 operations.
24#[derive(Error, Debug)]
25pub enum PyMinarrowError {
26 /// Error from MinArrow core library.
27 #[error("MinArrow error: {0}")]
28 Minarrow(#[from] minarrow::enums::error::MinarrowError),
29
30 /// Error during FFI operations.
31 #[error("FFI error: {0}")]
32 Ffi(String),
33
34 /// Type conversion error.
35 #[error("Type error: {0}")]
36 Type(String),
37
38 /// Unsupported Arrow type.
39 #[error("Unsupported type: {0}")]
40 UnsupportedType(String),
41
42 /// PyArrow import error.
43 #[error("PyArrow error: {0}")]
44 PyArrow(String),
45}
46
47impl From<PyMinarrowError> for PyErr {
48 fn from(err: PyMinarrowError) -> PyErr {
49 match err {
50 PyMinarrowError::Type(msg) => PyTypeError::new_err(msg),
51 PyMinarrowError::UnsupportedType(msg) => PyTypeError::new_err(msg),
52 PyMinarrowError::Ffi(msg) => PyRuntimeError::new_err(msg),
53 PyMinarrowError::PyArrow(msg) => PyValueError::new_err(msg),
54 PyMinarrowError::Minarrow(e) => {
55 // Map MinArrow errors to appropriate Python exceptions
56 match e {
57 minarrow::enums::error::MinarrowError::TypeError { .. } => {
58 PyTypeError::new_err(e.to_string())
59 }
60 minarrow::enums::error::MinarrowError::IncompatibleTypeError { .. } => {
61 PyTypeError::new_err(e.to_string())
62 }
63 minarrow::enums::error::MinarrowError::IndexError(_) => {
64 pyo3::exceptions::PyIndexError::new_err(e.to_string())
65 }
66 minarrow::enums::error::MinarrowError::Overflow { .. } => {
67 pyo3::exceptions::PyOverflowError::new_err(e.to_string())
68 }
69 _ => PyRuntimeError::new_err(e.to_string()),
70 }
71 }
72 }
73 }
74}
75
76/// Result type alias for minarrow-pyo3 operations.
77pub type PyMinarrowResult<T> = Result<T, PyMinarrowError>;