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

Cannot define a custom error using thiserror crate #4

Open
Luni-4 opened this issue Jan 18, 2024 · 7 comments
Open

Cannot define a custom error using thiserror crate #4

Luni-4 opened this issue Jan 18, 2024 · 7 comments

Comments

@Luni-4
Copy link

Luni-4 commented Jan 18, 2024

I'm trying to define a custom error through thiserror crate using this code

use thiserror::Error;

///Error types
#[derive(Debug, Error)]
pub enum Error {
    /// License not parsed correctly.
    #[error("License not parsed correctly")]
    InvalidLicense(#[from] license::ParseError),
}

/// A specialized `Result` type.
pub type Result<T> = ::std::result::Result<T, Error>;

but it seems that ParseError cannot be retrieved using #[from] attribute

@evenorog
Copy link
Owner

Thanks, will fix this when error is available in core.

rust-lang/rust#103765

@Luni-4
Copy link
Author

Luni-4 commented Jan 19, 2024

Out of curiosity, why do we need to wait until that issue will be closed? Can we use for now an intermediate solution? I am going to take minijinja as direct example since I have used that crate recently 😄

Here how an error is handled

An I can add #[from] minijinja::Error to my custom error.

@evenorog
Copy link
Owner

I have never used thiserror so I don't know how it works, but I'm assuming #[from] requires the Error trait to be implemented for this to work? Or does it require another trait to be implemented?

This library is no-std so I currently can't implement Error without adding additional feature gating for std and alloc.

@Luni-4
Copy link
Author

Luni-4 commented Jan 19, 2024

Ahh, if this library is considering even a no-std environment we cannot implement thiserror because it's a derive macro for std::error::Error trait. There is a workaround but they advise against using that.

In your opinion, what would a possible hack be to fix my problem? For now, I have introduced custom errors through map_err, but it does not seem a good solution eh eh

As an alternative, I might also try snafu, an error handling library which also considers no-std environment

@evenorog
Copy link
Owner

I'm only guessing but if it is the from attribute that fails, maybe you can implement the From trait manually?

impl From<ParseError> for Error {
   fn from(e: ParseError) -> Error {
       Error::InvalidLicense(e)
   }
}

Or is it the error attribute that fails?

@Luni-4
Copy link
Author

Luni-4 commented Jan 19, 2024

Yep, in that case works, just wanted to use the #[from] to simplify the code a bit, but it's okay for me this solution. Thanks a lot!

@Luni-4
Copy link
Author

Luni-4 commented Jan 19, 2024

The error I get with the #[from] attribute is:

src/error.rs:20:22
    |
20  |     InvalidLicense(#[from] license::ParseError),
    |                      ^^^^ method cannot be called on `&ParseError` due to unsatisfied trait bounds
    |
   ::: /home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/license-3.2.0/src/lib.rs:159:1
    |
159 | pub struct ParseError(());
    | ---------------------
    | |
    | doesn't satisfy `ParseError: AsDynError<'_>`
    | doesn't satisfy `ParseError: StdError`
    |
    = note: the following trait bounds were not satisfied:
            `ParseError: StdError`
            which is required by `ParseError: AsDynError<'_>`
            `&ParseError: StdError`
            which is required by `&ParseError: AsDynError<'_>`

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

No branches or pull requests

2 participants