Skip to content

Feature request: context() and with_context() on Result and Option #42

@danielparks

Description

@danielparks

I just switched from Snafu to thiserror, and on the whole my code has become a lot cleaner and clearer.

One thing that I miss is Snafu’s context() method on Result. It works much like the context() method in anyhow, but it allows adding structured context to an error. My current use case:

#[derive(Debug, Error)]
enum MyError {
    // . . .
    #[error("failed reading page {path:?}")]
    ReadPageFile { source: io::Error, path: PathBuf },
}

impl MyError {
    #[allow(non_snake_case)]
    fn ReadPageFileMap(path: &Path) -> Box<dyn FnOnce(io::Error) -> MyError> {
        let path = PathBuf::from(path);
        Box::new(|source: io::Error| MyError::ReadPageFile { source, path })
    }
}

fn read_from(path: &Path) -> Result<Page> {
    let raw = fs::read_to_string(path)
        .map_err(MyError::ReadPageFileMap(path))?;
    // . . .
}

Put more simply, I just want a convenient way to convert an io::Error into MyError::ReadPageFile while adding a path field to it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions