Get the downcast and underlying error message of the enum
0cv opened this issue · 1 comments
0cv commented
I've many Enums Errors and I find myself writing a list of if/else going through each of these enums in order to downcast the error to the right type, but for most cases, the only aspect I'm interested in is to know:
1- Which enum / variant caused the error
2- Retrieve the #[error(...)]
message
So I've to write something like:
if let Some(my_error) = e.downcast_ref::<EnumError1>() {
// my_error has now the underlying data of EnumError1
} else if let Some(my_error) = e.downcast_ref::<EnumError2>() {
// my_error has now the underlying data of EnumError2
} else if .....
Is there any more idiomatic way to achieve 1- and 2- than to go through all the enums (and especially not forget any, since it's not a proper match)?
dtolnay commented
This crate does not sound like the right fit for the use case you are describing. With a caller that needs to exhaustively handle a range of distinct error conditions, representing the error as an enum would be preferred.
match e {
MyError::Error1(e) => ...
MyError::Error2(e) => ...
}