knowhow 0.1.0

Error with context and with known source error
Documentation
  • Coverage
  • 25%
    1 out of 4 items documented1 out of 1 items with examples
  • Size
  • Source code size: 8.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 340.52 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • istudyatuni

Knowhow

Like anyhow but you know underlying error

use knowhow::{Context, Contextual};

#[derive(Debug, thiserror::Error)]
enum Error {
    #[error("failed: {0}")]
    Failed(&'static str),
}

fn fallible() -> Result<(), Error> {
    Err(Error::Failed("message"))
}

fn foo() -> Result<(), Contextual<Error>> {
    fallible()
        // when attaching more context, you need to help infer error type
        .context::<_, Error>("more context")
        .context("processing fallible function")?;

    Ok(())
}

fn foo_no_context() -> Result<(), Contextual<Error>> {
    fallible()?;
    Ok(())
}

fn main() {
    let e = foo().unwrap_err();
    std::assert_matches!(e.source(), Error::Failed("message"));
    println!("{e}");

    let e = foo_no_context().unwrap_err();
    std::assert_matches!(e.source(), Error::Failed("message"));
    println!("{e}");
}
Error: processing fallible function
Caused by:
  more context
  failed: message
Error: failed: message

Currently only &'static str-ings can be used in context()