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)] // or PyException
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 excepti…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@ChristopherRabotin
Comment options

@austin362667
Comment options

@ChristopherRabotin
Comment options

@austin362667
Comment options

@Icxolu
Comment options

Icxolu Jul 17, 2024
Collaborator

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
3 participants