Skip to content

Deriving BaseException for custom error enums #4165

Discussion options

You must be logged in to vote

I think you want to expose a custom exception type and use that while converting you library error into PyErr. Perhaps you can do something like this:

// your library error type
pub enum EpochError { ... }

// The exception type that gets exposed to Python
// alternativly the `create_exception!` macro should also work
#[pyclass]
#[pyo3(name = "EpochError", extends = PyBaseException)]
pub struct PyEpochError {}

#[pymethods]
impl PyEpochError {
    #[new]
    #[pyo3(signature = (*_args, **_kwargs))]
    fn new(_args: Bound<'_, PyTuple>, _kwargs: Option<Bound<'_, PyDict>>) -> Self {
        Self {}
    }
}

// convert you library error into a PyErr using the custom exception type
impl From<E…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ChristopherRabotin
Comment options

Answer selected by ChristopherRabotin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants