-
Notifications
You must be signed in to change notification settings - Fork 6
Description
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.