Skip to content

Converting from anyhow::Error #31

@akshayknarayan

Description

@akshayknarayan

Hi,
I am porting multiple crates from anyhow to eyre, so I have code that looks something like this:

fn foo() -> Result<(), anyhow::Error> {
  Ok(())
}

fn bar() -> Result<(), eyre::Report> {
  let f = foo().map_err(|e| eyre::Report::new(e))?;
  Ok(())
}

This, as expected, gives

error[E0277]: the trait bound `anyhow::Error: std::error::Error` is not satisfied
  --> src/lib.rs:6:49
   |
6  |     let f = foo().map_err(|e| eyre::Report::new(e))?;
   |                                                 ^ the trait `std::error::Error` is not implemented for `anyhow::Error`
   |
  ::: /home/akshayn/.cargo/registry/src/github.com-1ecc6299db9ec823/eyre-0.6.0/src/error.rs:21:12
   |
21 |         E: StdError + Send + Sync + 'static,
   |            -------- required by this bound in `eyre::error::<impl eyre::Report>::new`

error: aborting due to previous error

Since anyhow::Error and eyre::Report are so similar, is there a better way to construct an eyre::Report mid-stack than this, which is sad and throws out the context:

fn foo() -> Result<(), anyhow::Error> {
  Ok(())
}

fn bar() -> Result<(), eyre::Report> {
  let f = foo().map_err(|e| eyre::eyre!("{:?}", e))?;
  Ok(())
}

Thanks for the help!

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