How to map one source Error type to different SNAFU Error variants? #402
-
This seems like it should be a not-too-uncommon situation, so I apologize if it's a simple question, but I can't figure out the best way to handle it. I have a particular library error type that I need to map to different variants of my application's error type, depending on the contents of the error, and I want that to happen every time that error is encountered. To give some more context: Before I started using SNAFU, I used this impl From<ZBusError> for Error {
fn from(value: ZBusError) -> Self {
match value {
ZBusError::MethodError(err, msg, full_msg) =>
match err.as_str() {
"net.connman.iwd.Busy" => Error::WifiBusy(msg),
"net.connman.iwd.Failed" => Error::IwdFailure(msg),
_ => Error::DBus(ZBusError::MethodError(err, msg, full_msg)),
}
_ => Error::DBus(value)
}
}
} Basically, With SNAFU, the two ways I've thought of to handle this are:
The first almost seems like it would work, except if there's an implicitly-generated backtrace or The second way wouldn't suffer from that problem, but it'd lead to a lot of repetitive code. There are many places I need to handle this type of error. Is there a better way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Would applying |
Beta Was this translation helpful? Give feedback.
Would applying
#[track_caller]
to thefn from
help?