-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Retrieve full error message from type-erased anyhow::Error #123
Comments
The easiest would be to store your catch-all as Alternatively you can iterate causes of an arbitrary error like this: use anyhow::anyhow;
use std::error::Error;
fn main() {
let err = anyhow!("one").context("two").context("three");
print(&err.into());
}
fn print(err: &Box<dyn Error + Send + Sync + 'static>) {
for e in anyhow::Chain::new(err.as_ref()) {
println!("{}", e);
}
} |
Indeed, I have that implemented on the development branch, and it of course works perfectly. The consequence it that it bakes
Thanks, I'll use this if we decide to keep |
To expand on this, I see value in being able to obtain the whole message exactly as If you don't think such a function is desirable, feel free to close this issue, since the problem I was having is resolved with your last comment. |
I'll go ahead and close this. #123 (comment) are still the 2 recommended approaches. |
I am dealing with an error enum that has a catch-all variant that uses
Box<dyn Error + Send + Sync + 'static>
. This works withanyhow::Error
just fine (withinto()
or?
), but there is a problem when reporting the error. When showing the error message, the code usesDisplay
orto_string()
on theBox<dyn Error>
. With e.g. an IO error this is OK, but withanyhow::Error
it prints just the outermost context. In that particular place I need it to print the full list of causes as well as the root error.I tried the following:
Error::downcast_ref()
to downcastBox<dyn Error + ...>
toanyhow::Error
- doesn't compile becauseanyhow::Error
doesn't implementstd::error::Error
.format!("{:#}", err)
- doesn't appear to make a difference in the output.format!("{:?}", err)
- works, but only foranyhow::Error
- for all other errors, it prints the low-level output meant for debugging, and I can't detect whether the box holds ananyhow::Error
.anyhow::Error
from the box, and then use the debug output - doesn't compile because the constructor consumes the box, and I only have a reference to the box. (I need to generate the message fromDisplay::fmt
andDebug::fmt
on my error, where I get&self
.)I could easily fix the problem if I could do any of the following:
Error
trait to obtain the full error message.&Box<dyn Error + ...>
toOption<&anyhow::Error>
(returningSome
ifanyhow::Error
is inside).Box<dyn Error + ...>
, detect whether ananyhow::Error
is inside;Is that possible, or is there some other approach that I haven't considered to print the full error information?
The text was updated successfully, but these errors were encountered: