Hi folks,
I learned about error handling, how to implement Error and Display traits for my custom error enums and how to implement From trait so I can use ?
. And I started using thiserror
so I don't need to do any of that
What I'm not sure is what to add to the inner (the most low-level) enum. In all examples, I find people just moving system errors into a parent enum but in my case, I have some parsing/text manipulation functions that use MyError1
like this:
#[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)]
enum MyError1 {
#[error("foo")]
FirstError,
#[error("bar")]
SecondError,
}
#[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)]
enum MyError2 {
#[error("abcdef")]
ThirdError(MyError1),
#[error("12345")]
FourthError(MyError1),
}
// custom From since I want to map one enum to another and thiserror can't do that
impl From<MyError1> for MyError2 {
fn from(err: MyError1) -> Self {
match err {
MyError1::FirstError => MyError2::ThirdError(err),
MyError1::SecondError => MyError2::FourthError(err),
}
}
}
and I'm not sure what to put inside.
In other words should I put a String
into the MyError1
?
enum MyError1 {
#[error("foo")]
FirstError(String),
#[error("bar")]
SecondError(String),
}
and then
match x {
"" => MyError1::FirstError("the input was empty")
...
}
or maybe a custom struct FirstError(CustomStruct)
?
Thank you.