You can’t create a subset of an enum directly, but splitting this up into multiple types works. You can have FunctionAError
with errors that function can produce and a variant for your common errors, and FunctionBError
which is similar:
#[derive(Debug, Error)]
enum MyErrorCommon {
#[error("bad value ({0})")]
MyErrorCommon(String),
}
#[derive(Debug, Error)]
enum FunctionAError {
#[error("error a")]
MyErrorA,
Common(#[from] MyErrorCommon),
}
// and same for FunctionBError
The try operator (?
) will automatically use From
impls to convert errors for you as well. If a function returns a result containing MyErrorCommon
in your function and you use ?
on it, it gets converted to that function’s error type for you. thiserror
generates the From
impl for you if you use #[from]
.
This is even worse because it can happen in prod without you ever triggering this case. For some projects, it doesn’t matter because the impact of a bug is small. For most, you put a subpar, buggy experience in front of your users, waste more time looking for the cause and debugging later with upset users, and at worst cause actual damages (depending on the project anyway).