shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.45k stars 61 forks source link

Enhance GenerateImplicitData to provide the optional source error #349

Closed shepmaster closed 2 years ago

shepmaster commented 2 years ago

This should allow us to capture a backtrace only when the underlying error didn't already capture one.

General idea:

trait GenerateImplicitData {
    fn generate() -> Self; // Existing fn

    fn generate_with_source(e: &dyn Error) -> Self {
        Self::generate() // default impl delegates to existing fn
    }
}

This should allow us to be semver compatible. Would we want to combine this to one function with Option<&dyn Error> in a breaking change later?

Then we'd implement it for backtrace like...

impl GenerateImplicitData for Option<Backtrace> {
    fn generate() { todo!("...") }

    fn generate_with_source(e: &dyn Error) -> Self {
        // appropriate checks for feature enabled
        if e.request_ref::<Backtrace>().is_none() {
            Self::generate() // delegates to existing fn
        }
    }
}

Should we create another type that bakes the Option in?