thiserror icon indicating copy to clipboard operation
thiserror copied to clipboard

Unable to get `#[from]` + `backtrace` example to compile

Open metasim opened this issue 2 years ago • 5 comments
trafficstars

I have a use case where this example from the README is exactly what I need:

#[derive(Error, Debug)]
pub enum MyError {
    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    },
}

However, when I compile it (Rust Playground link) I get the following error:

no `ThiserrorProvide` in `__private`
   Compiling playground v0.0.1 (/playground)
error[E0432]: unresolved import `thiserror`
   --> src/lib.rs:5:10
    |
5   | #[derive(Error, Debug)]
    |          ^^^^^ no `ThiserrorProvide` in `__private`
    |
note: found an item that was configured out
   --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/thiserror-1.0.48/src/lib.rs:256:29
    |
256 |     pub use crate::provide::ThiserrorProvide;
    |                             ^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0658]: use of unstable library feature 'error_generic_member_access' --> src/lib.rs:5:10 | 5 | #[derive(Error, Debug)] | ^^^^^ | = note: see issue #99301 https://github.com/rust-lang/rust/issues/99301 for more information = note: this error originates in the derive macro Error (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: MyError doesn't implement std::fmt::Display --> src/lib.rs:6:10 | 6 | pub enum MyError { | ^^^^^^^ MyError cannot be formatted with the default formatter | = help: the trait std::fmt::Display is not implemented for MyError = note: in format strings you may be able to use {:?} (or {:#?} for pretty-print) instead note: required by a bound in std::error::Error --> /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/error.rs:32:1

metasim avatar Oct 17 '23 17:10 metasim

You need to use the nightly compiler and enable the error_generic_member_access feature. See working playground. Thiserror's documentation should be more clear that this depends on a nightly feature.

ZZT32 avatar Nov 16 '23 21:11 ZZT32

Also, in case the previous solution doesn't work, make sure you're on a recent nightly version. I was on nightly-2023-06-24 and I was confused why thiserror wasn't compiling.

Bauxitedev avatar Nov 17 '23 10:11 Bauxitedev

Perhaps this feature should be under a feature flag or cfg associated with error_generic_member_access? Or at least be clarified in the documentation?

metasim avatar Nov 28 '23 15:11 metasim

I just had this exact same problem and was very confused as the nightly + feature gate requirement isn't mentioned in the documentation.

bart-sensirion-qm avatar Dec 19 '23 12:12 bart-sensirion-qm

I gave up on this, I ended up doing something like this to work around the limitation: https://github.com/loco-rs/loco/blob/master/src/errors.rs#L129-L139

in other words, code the from yourself, and capture the backtrace manually, which is the same approach taken by candle. I recommend doing a search around github to find some approaches that suit you -- I didn't find anything else that worked well for stable.

I needed to control the backtrace because I produce clean, pretty backtraces in Loco which look like this:

https://github.com/loco-rs/loco/issues/41#issuecomment-1860064976

Which also include backtrace filtering.

jondot avatar Dec 19 '23 14:12 jondot