pyo3_error

Struct PyErrChain

Source
pub struct PyErrChain { /* private fields */ }
Expand description

PyErrChain wraps a PyErr together with its causality chain.

Unlike PyErr, PyErrChain’s implementation of std::error::Error provides access to the error cause through the std::error::Error::source method.

Note that since PyErrs can be readily cloned, the PyErrChain only captures the causality chain at the time of construction. Calling PyErr::set_cause on a clone of the wrapped error after construction will thus not update the chain as captured by this PyErrChain.

Implementations§

Source§

impl PyErrChain

Source

pub fn new<T: Into<Box<dyn Error + 'static>>>(py: Python<'_>, err: T) -> Self

Create a new PyErrChain from err.

The error’s causality chain, as expressed by std::error::Error::source, is translated into a PyErr::cause chain.

If any error in the chain is a PyErrChain or a PyErr, it is extracted directly. All other error types are translated into PyErrs using PyException::new_err with format!("{}", err).

If you want to customize the translation from std::error::Error into PyErr, please use Self::new_with_translator instead.

This constructor is equivalent to chaining Self::pyerr_from_err with Self::from_pyerr.

Source

pub fn new_with_translator<E: Into<Box<dyn Error + 'static>>, T: AnyErrorToPyErr, M: MapErrorToPyErr>( py: Python<'_>, err: E, ) -> Self

Create a new PyErrChain from err using a custom translator from std::error::Error to PyErr.

The error’s causality chain, as expressed by std::error::Error::source, is translated into a PyErr::cause chain.

If any error in the chain is a PyErrChain or a PyErr, it is extracted directly. All other error types first attempt to be translated into PyErrs using the AnyErrorToPyErr and MapErrorToPyErr. As a fallback, all remaining errors are translated into PyErrs using PyException::new_err with format!("{}", err).

This constructor is equivalent to chaining Self::pyerr_from_err_with_translator with Self::from_pyerr.

Source

pub fn pyerr_from_err<T: Into<Box<dyn Error + 'static>>>( py: Python<'_>, err: T, ) -> PyErr

Transform an err implementing std::error::Error into a PyErr that preserves the error’s causality chain.

The error’s causality chain, as expressed by std::error::Error::source, is translated into a PyErr::cause chain.

If any error in the chain is a PyErrChain or a PyErr, it is extracted directly. All other error types are translated into PyErrs using PyException::new_err with format!("{}", err).

If you want to customize the translation from std::error::Error into PyErr, please use Self::pyerr_from_err_with_translator instead.

Source

pub fn pyerr_from_err_with_translator<E: Into<Box<dyn Error + 'static>>, T: AnyErrorToPyErr, M: MapErrorToPyErr>( py: Python<'_>, err: E, ) -> PyErr

Transform an err implementing std::error::Error into a PyErr that preserves the error’s causality chain using a custom translator.

The error’s causality chain, as expressed by std::error::Error::source, is translated into a PyErr::cause chain.

If any error in the chain is a PyErrChain or a PyErr, it is extracted directly. All other error types first attempt to be translated into PyErrs using the AnyErrorToPyErr and MapErrorToPyErr. As a fallback, all remaining errors are translated into PyErrs using PyException::new_err with format!("{}", err).

Source

pub fn from_pyerr(py: Python<'_>, err: PyErr) -> Self

Wrap a PyErr and capture its causality chain, as expressed by PyErr::cause.

Source

pub fn into_pyerr(self) -> PyErr

Extract the wrapped PyErr.

Source

pub const fn as_pyerr(&self) -> &PyErr

Get a reference to the wrapped PyErr.

Note that while PyErr::set_cause can be called on the returned PyErr, the change in causality chain will not be reflected in this PyErrChain.

Source

pub fn cause(&self) -> Option<&PyErr>

Get a reference to the cause of the wrapped PyErr.

Note that while PyErr::set_cause can be called on the returned PyErr, the change in causality chain will not be reflected in this PyErrChain.

Source

pub fn clone_ref(&self, py: Python<'_>) -> Self

Clone the PyErrChain.

This requires the GIL, which is why PyErrChain does not implement Clone.

Note that all elements of the cloned PyErrChain will be shared using reference counting in Python with the existing PyErrChain self.

Trait Implementations§

Source§

impl Debug for PyErrChain

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PyErrChain

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for PyErrChain

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<PyErr> for PyErrChain

Source§

fn from(err: PyErr) -> Self

Converts to this type from the input type.
Source§

impl From<PyErrChain> for PyErr

Source§

fn from(err: PyErrChain) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,