Skip to content

[Feature Request] Recover a sub Exn error from children frames #40

@macier-pro

Description

@macier-pro

Context

I'm trying to extract structured data from errors for programmatic handling, but going further by getting a sub Exn from the error tree. Currently, the error tree is lost by using downcast_ref.

The closest I can get is by walking through the error frame to extract a particular error, but it would only return a reference to the underlying error, losing the error tree.

fn find_error<T: Error + 'static>(exn: &Exn<impl Error + Send + Sync>) -> Option<&T> {
    fn walk<T: Error + 'static>(frame: &Frame) -> Option<&T> {
        if let Some(e) = frame.error().downcast_ref::<T>() {
            return Some(e);
        }
        frame.children().iter().find_map(walk)
    }
    walk(exn.frame())
}

This example comes from https://github.com/fast/exn/blob/main/examples/src/downcast.rs#L63C1-L71C2

Expectations

Let's say the error tree looks like this :

MainError -> ErrorA -> ErrorB

Using find_error, we can get a &ErrorA using a downcast_ref. However, the error tree is lost.

I would like to retrieve a Exn<ErrorA> to use it as I would with other functions I already implemented.
This way, we can always process it later and keep the info that ErrorA was caused by ErrorB.

Alternatives

I could just work with the frame tree, but it's really not user-friendly nor as convenient as having a proper Exn.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions