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 PyErr
s 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
impl PyErrChain
Sourcepub fn new<T: Into<Box<dyn Error + 'static>>>(py: Python<'_>, err: T) -> Self
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 PyErr
s
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
.
Sourcepub fn new_with_translator<E: Into<Box<dyn Error + 'static>>, T: AnyErrorToPyErr, M: MapErrorToPyErr>(
py: Python<'_>,
err: E,
) -> Self
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 PyErr
s using the AnyErrorToPyErr
and MapErrorToPyErr
.
As a fallback, all remaining errors are translated into PyErr
s using
PyException::new_err
with format!("{}", err)
.
This constructor is equivalent to chaining
Self::pyerr_from_err_with_translator
with Self::from_pyerr
.
Sourcepub fn pyerr_from_err<T: Into<Box<dyn Error + 'static>>>(
py: Python<'_>,
err: T,
) -> PyErr
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 PyErr
s
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.
Sourcepub fn pyerr_from_err_with_translator<E: Into<Box<dyn Error + 'static>>, T: AnyErrorToPyErr, M: MapErrorToPyErr>(
py: Python<'_>,
err: E,
) -> PyErr
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 PyErr
s using the AnyErrorToPyErr
and MapErrorToPyErr
.
As a fallback, all remaining errors are translated into PyErr
s using
PyException::new_err
with format!("{}", err)
.
Sourcepub fn from_pyerr(py: Python<'_>, err: PyErr) -> Self
pub fn from_pyerr(py: Python<'_>, err: PyErr) -> Self
Wrap a PyErr
and capture its causality chain, as expressed by
PyErr::cause
.
Sourcepub fn into_pyerr(self) -> PyErr
pub fn into_pyerr(self) -> PyErr
Extract the wrapped PyErr
.
Sourcepub const fn as_pyerr(&self) -> &PyErr
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
.
Sourcepub fn cause(&self) -> Option<&PyErr>
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
.
Sourcepub fn clone_ref(&self, py: Python<'_>) -> Self
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
.