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

anyhow::Error does not implement std::error::Error #136

Closed
L-as opened this issue Jan 12, 2021 · 5 comments
Closed

anyhow::Error does not implement std::error::Error #136

L-as opened this issue Jan 12, 2021 · 5 comments

Comments

@L-as
Copy link

L-as commented Jan 12, 2021

Is this an oversight or is there some inherent limitation that causes this?

Thanks

@dtolnay
Copy link
Owner

dtolnay commented Jan 12, 2021

This is intentional. That impl would be incoherent with the existing pair of impls impl From<T> for T (libcore) and impl<E> From<E> for anyhow::Error where E: std::error::Error + Send + Sync + 'static (? support).

error[E0119]: conflicting implementations of trait `std::convert::From<anyhow::Error>` for type `anyhow::Error`:
   --> src/error.rs:313:1
    |
313 | / impl<E> From<E> for Error
314 | | where
315 | |     E: std::error::Error + Send + Sync + 'static,
316 | | {
...   |
320 | |     }
321 | | }
    | |_^
    |
    = note: conflicting implementation in crate `core`:
            - impl<T> std::convert::From<T> for T;

@dtolnay dtolnay closed this as completed Jan 12, 2021
@L-as
Copy link
Author

L-as commented Jan 12, 2021 via email

@dtolnay
Copy link
Owner

dtolnay commented Jan 12, 2021

You can generate such a function using thiserror:

use thiserror::Error;

#[derive(Error, Debug)]
#[error(transparent)]
struct Error(anyhow::Error);

Now Error(...) is a function with the signature anyhow::Error -> impl std::error::Error where the resulting error impl has a source/display/backtrace consistent with the argument.

@L-as
Copy link
Author

L-as commented Jan 12, 2021 via email

@cipriancraciun
Copy link

Why I understand the technical reason why the Rust compiler wouldn't allow anyhow::Error to also implement std::error::Error, I wouldn't just blame the compiler, but I would instead take a second look at the impl From<E> for anyhow::Error where E : std::error::Error ....

At the moment the anyhow library makes it trivial to just convert from any std::error::Error into an anyhow::Error without any other additional trait import (because it relies on From<E>), which is great for the library developer using anyhow! All errors, no mater of the type, are quickly gobbled into a generic anyhow::Error.

However, downstream, when another developer needs to use a library that relies on anyhow::Error, and if he chooses some other "great error library" he just hits a wall and has to treat anyhow special...

So, any error is easily gobbled up into an anyhow::Error, however once there you can't rely anymore on traits working. Thus anyhow doesn't play nice with other error libraries out there.


Thus, could we find a compromise where anyhow::Error does actually implement std::error::Error like any good behaved error? :)

(Or at least a workaround for other "great error library" implementers out there?)

Repository owner locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants