reveal

Crates.ioreveal
lib.rsreveal
version0.1.1
created_at2023-05-15 05:50:21.72214+00
updated_at2025-08-30 14:23:54.77717+00
descriptionAdd location and message to errors
homepage
repositoryhttps://github.com/luoshuqi/reveal
max_upload_size
id864748
size14,490
(luoshuqi)

documentation

README

Reveal

reveal is a Rust library designed to automatically add context to errors, making it easier to trace and debug issues by including file names, line numbers, and function names in error messages.

Install

cargo add reveal

Features

  • Automatically adds context to errors.
  • Works with any error type that implements std::error::Error.
  • Provides detailed error tracebacks.

Examples

Here's a basic example demonstrating how to use the map_err macro to add context to errors:

use reveal::map_err;

fn main() {
    if let Err(e) = run() {
        println!("{}", e);
    }
}

#[map_err]
fn run() -> reveal::Result<()> {
    App.load_config()?;
    Ok(())
}

struct App;

#[map_err]
impl App {
    #[map_err]
    fn load_config(&self) -> reveal::Result<Vec<u8>> {
        let config = file_get_contents("non_exists_config.toml")?;
        Ok(config)
    }
}

#[map_err]
fn file_get_contents(path: &str) -> reveal::Result<Vec<u8>> {
    fn read(path: &str) -> reveal::Result<Vec<u8>> {
        Ok(std::fs::read(path)?)
    }

    read(path).map_err(|err| err.context(format!("Failed to read file `{}`", path)))
}

Output:

When the above example is run, it produces the following output:

Failed to read file `non_exists_config.toml`: No such file or directory (os error 2)
#0 src/main.rs:29 demo::file_get_contents()
#1 src/main.rs:21 demo::App::load_config()
#2 src/main.rs:11 demo::run()
Commit count: 1

cargo fmt