Closed vakond closed 9 months ago
context
, when used on a Result
, expects to wrap the inner error. Your error variant doesn't have a source
field, so it's trying to use the NoneError
which is used when context
is used on an Option
.
Theoretically you'd want to have something like:
XXXX { which: String, source: Error },
This cannot work as it makes an infinitely sized type as it contains itself.
More idiomatic SNAFU will have you creating more error types and / or variants. Additionally, you do not need to (and should not) call .to_string()
inside of context
as it unconditionally allocates memory, even when there's no error.
use snafu::{ResultExt, Snafu};
#[derive(Debug, Snafu)]
enum OuterError {
#[snafu(display("Xxxx '{which}'"))]
Xxxx { source: InnerError, which: String },
}
#[derive(Debug, Snafu)]
struct InnerError;
fn main() {
let _ = caller();
}
fn caller() -> Result<(), OuterError> {
test().context(Xxxx { which: "xxxx" })
}
fn test() -> Result<(), InnerError> {
Ok(())
}
Great! Thank you for this excellent explanation. I suspected there should be source
, and tried Box<Error>
without success. Now I understand why.
Minimal example. I just want to use context:
But compilation fails with this message:
What's wrong?