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

Converting from anyhow::Error #31

Closed
akshayknarayan opened this issue Jul 10, 2020 · 9 comments · May be fixed by #127
Closed

Converting from anyhow::Error #31

akshayknarayan opened this issue Jul 10, 2020 · 9 comments · May be fixed by #127

Comments

@akshayknarayan
Copy link
Contributor

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!

@yaahc
Copy link
Collaborator

yaahc commented Jul 10, 2020

the best way to convert between the two would be to first convert the anyhow::Error to a Box<dyn Error> and then convert that to an Eyre report with the macro. that way you'll preserve the chain of sources at least, though you'll still lose the backtrace that anyhow captured originally.

@akshayknarayan
Copy link
Contributor Author

that makes sense, thanks!

@Arjentix
Copy link

Just for future readers:

foo().map_err(|err| eyre!(Box::new(err))

@pksunkara
Copy link
Contributor

though you'll still lose the backtrace that anyhow captured originally.

verbosity of code to convert

With both the above issues in mind, is it worth adding some code in this crate under anyhow-compat feature to add support to use ? on anyhow errors to convert them eyre errors easily?

@ppoliani
Copy link

ppoliani commented Jun 3, 2023

map_err(|err| eyre!(Box::new(err))

Alternatively you can wrap this inside a declarative macro

map_err!(foo())?;

#[macro_export]
macro_rules! map_err {
  ($fun:expr) => {
    $fun.map_err(|e| eyre!(Box::new(e)))
  }
}

@pksunkara
Copy link
Contributor

@yaahc Please let me know if you welcome a PR

@yaahc
Copy link
Collaborator

yaahc commented Jun 6, 2023

@yaahc Please let me know if you welcome a PR

I've no objections to adding an anyhow_compat feature, PRs welcome

@thewh1teagle
Copy link

What's the status of this feature?
I would like to use ? on anyhow results on functions which return eyre::Result.

@Danvil
Copy link

Danvil commented May 30, 2024

I would also be interested in that. Some other coded in my project uses anyhow and manually mapping the errors is quite a pain..

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

Successfully merging a pull request may close this issue.

7 participants