You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found that std::result::Result<T, error_stack::Report<anyhow::Error>> doesn't implement error_stack::IntoReport, so if I try to take an error from a crate that uses anyhow and call .report(), i get some errors. Is this an incompatibility with anyhow, or did I set up my project wrong?
I made a custom error type that implements std::error::Error and std::fmt::Display and tried implementing std::convert::From<anyhow::Error> for my custom type. But to no avail.
Is there a better way to do this? This is fine I suppose, it just doesn't feel ergonomic and I expect to encounter this difficulty quite frequently. It would be nice if all MyError had to do was implement From.
So the main problem you're encountering with this is that anyhow isn't really designed to be a library friendly implementation. It is very opinionated, which is great when you're only using anyhow (which is what it's designed for), where you change your Error types to anyhow::Error.
They've made an active decision to not implement std::error::Error for anyhow::Error which basically forces you to only use their error type in signatures and such. (Again, not a criticism, it's just the philosophy of their library, it's a trade-off for other benefits).
We've provided a blanket implementation of Context for std::error::Error for convenience but because of the above this doesn't cover anyhow::Error. So at the moment your approach might be the best solution if you want to continue to use anyhow, however I'd consider anyhow and error-stack as different approaches, rather than something to be used in tandem.
Maybe some of the comments in this discussion will help capture the philosophy too :)
Activity
Alfred-Mountfield commentedon Jul 6, 2022
Are you able to fix some of the formatting on your post? I'm finding it a little difficult to know what types you're dealing with.
For example is this:
meant to be
std::result::Result<T, error_stack::Report<anyhow::Error>>?Because if so it's likely failing because you're trying to turn a
Reportinto aReport.sdwoodbury commentedon Jul 6, 2022
@Alfred-Mountfield thanks for the help. I'm still learning how to decipher larger error messages. You're correct.
in case anyone else has this question, I'll show the work around I found, after I understood what was happening:
matchon theanyhow::ErrorErr, turn it intoErr(Report::new(e.into()))example:
also had to do this:
Is there a better way to do this? This is fine I suppose, it just doesn't feel ergonomic and I expect to encounter this difficulty quite frequently. It would be nice if all
MyErrorhad to do was implementFrom.Alfred-Mountfield commentedon Jul 6, 2022
Thanks for the expanded code, it helps a lot!
So the main problem you're encountering with this is that
anyhowisn't really designed to be a library friendly implementation. It is very opinionated, which is great when you're only usinganyhow(which is what it's designed for), where you change yourErrortypes toanyhow::Error.They've made an active decision to not implement
std::error::Errorforanyhow::Errorwhich basically forces you to only use their error type in signatures and such. (Again, not a criticism, it's just the philosophy of their library, it's a trade-off for other benefits).We've provided a blanket implementation of
Contextforstd::error::Errorfor convenience but because of the above this doesn't coveranyhow::Error. So at the moment your approach might be the best solution if you want to continue to useanyhow, however I'd consideranyhowanderror-stackas different approaches, rather than something to be used in tandem.Maybe some of the comments in this discussion will help capture the philosophy too :)