Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downcasting on individual context layers #84

Open
danburkert opened this issue Apr 9, 2020 · 1 comment
Open

downcasting on individual context layers #84

danburkert opened this issue Apr 9, 2020 · 1 comment

Comments

@danburkert
Copy link

Hi, I would like to be able to downcast on the just the topmost layer of the context stack. Looking through the API, it doesn't appear that this is possible. It's nearly possible by using the underlying std::error::Error provided downcasting, instead of the anyhow downcasting, but anyhow wraps the context in a private ContextError. Even if it were public, I still don't think it would be possible since I know the C context type I want, but not the E error type.

I think I can work around this in a good enough way for my usecase by comparing my_anyhow_error.downcast_ref::<MyType>().unwrap().to_string() to my_anyhow_error.to_string(), which gives me a pretty good indication that the first instance of MyType in the stack is the topmost context layer, but I'm curious if it might be a generally useful feature for anyhow.

@oli-cosmian
Copy link

oli-cosmian commented Jul 20, 2020

Maybe in addition to the downcast_ref method there could be a downcast_context method that gives you a reference to the context if there is one and downcasting the context succeeded?

My workaround was to create a new error type with a cause field that points to the original anyhow::Error, but that seems undesirable as I now have multiple box layers in the errors. Basically I want https://docs.rs/anyhow/1.0.31/anyhow/trait.Context.html#effect-on-downcasting to work, but that doesn't work for me (maybe that demo requires an Option instead of a Result?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants