Skip to content

how to ingest anyhow::Error?  #755

Closed

Description

@sdwoodbury

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.

Activity

  1. Alfred-Mountfield commented on Jul 6, 2022

    @Alfred-Mountfield
    Contributor

    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:

    std::result::Result<T, error_stack::Reportanyhow::Error>

    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 Report into a Report.

  2. sdwoodbury commented on Jul 6, 2022

    @sdwoodbury
    Author

    @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:

    1. match on the anyhow::Error
    2. On Err, turn it into Err(Report::new(e.into()))

    example:

    let res: Result<_, MyError> = match thing_that_returns_anyhow_error {
        Ok(r) => Ok(r),
       Err(e) => Err(Report::new::(e.into()))
    };
    
    let thing_i_wanted = res.attach_printable(...)?;
    

    also had to do this:

    impl std::convert::From<anyhow::Error> for MyError {
        fn from(err: anyhow::Error) -> MyError {
            MyError { ...) }
        }
    } 
    

    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.

  3. Alfred-Mountfield commented on Jul 6, 2022

    @Alfred-Mountfield
    Contributor

    Thanks for the expanded code, it helps a lot!

    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 :)

  4. locked and limited conversation to collaborators on Jul 6, 2022
  5. converted this issue into a discussion #761 on Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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