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
Generic type parameter on error #79
Comments
The #[from] attribute (which implies #[source]) requires that the source error has a std::error::Error impl and Debug impl. For your use case would this work?— use std::error::Error as StdError;
use thiserror::Error;
#[derive(Error, Debug)]
enum Error<E: StdError + 'static> {
#[error("variant error")]
Variant,
#[error(transparent)]
Delegate(#[from] E),
} |
Ya, I didn't want to do that because then I have to litter the same bound everywhere I introduce I was hoping that the generated code localise that like here: impl<E: error::Error> error::Error for Error<E> |
The concern is an use std::error::Error as StdError;
use std::fmt::{Debug, Display};
use std::ops::Deref;
use thiserror::Error;
trait BoxedError: Debug + Display + Deref<Target = dyn StdError> {}
impl<E: Debug + Display + Deref<Target = dyn StdError>> BoxedError for E {}
#[derive(Error, Debug, Clone)]
enum Error<E: BoxedError> {
#[error(transparent)]
Delegate(#[from] E),
} and works with |
I may consider accepting an attribute to place an additional bound on just the generated std::error::Error impl. #[derive(Error, Debug)]
#[error(bound = std::error::Error + 'static)]
enum Error<E> {
#[error(transparent)]
Delegate(#[from] E),
} |
I suppose it would be fine to recognize the very specific case of |
That's intriguing! I haven't seen anything set up like that before, but ya that makes sense :) Would not want to be overly restrictive. I'm not sure what the advantages or disadvantages of the two above are, but I'd be happy with either 👍 If there's anything I could do to help with that I'd be happy to give it a shot at some point. |
If you're able to send a PR, I would gladly accept one for #79 (comment). |
I'll take a crack at it when I get a chance :) I'll ping questions here if I get lost |
The example described in #79 (comment) does not work with multiple For example: #[derive(Debug, thiserror::Error)]
pub enum MyError<E: std::error::Error + 'static> {
#[error("io: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
Inner(#[from] E),
} returns the following error:
is this because |
You're right @jeromegn. I didn't account for the generic parameter being allowed to be the same as an existing variant... So this might be not very feasible in the end :/ |
Is there anything preventing #107 from getting merged. Would love to be able to do this without having to write out implement myself. |
With #143, I believe that I've implemented the solution suggested in the above comment. |
Hi! Do you still have plan to support #[derive(Debug, ThisError)]
pub enum AVMError<E> {
#[error(transparent)]
DataStoreError(#[from] E),
} Upd: Sorry, I've just found #149, but I still can't compile code like that #[derive(Debug, ThisError)]
pub enum AVMError<E> {
#[error(transparent)]
RunnerError(#[from] RunnerError),
#[error(transparent)]
DataStoreError(#[from] E),
}
#[derive(Debug, ThisError)]
pub enum RunnerError {} with a error 76 | #[derive(Debug, ThisError)]
| ^^^^^^^^^
| |
| first implementation here
| conflicting implementation for `errors::AVMError<errors::RunnerError>` Are there any problems of having several Upd 2. Double sorry, I found out that it's generally impossible in Rust... :) |
Multiple See this playground for an example based on your above code. Honestly, if we can get that fixed in a reasonable, low-boilerplate way, the |
@Dessix Thank you, yes, that what I meant by impossible in Rust. I wanted two Btw, I workarounded it with one |
I ran into an error when having a generic parameter in my error type. So the setup is that I want to have a variant in my error enum such that it delegates to a different error, like below:
Unfortunately, I get an error about
E
not having aDebug
instance:Could this be possible to do or is there something that is restricting this use case? 🤔
For now, I'm going to write out the implementations by hand, but it would be cool to delegate that work to
thiserror
.Thanks for any help 🙏
The text was updated successfully, but these errors were encountered: