erdp

Crates.ioerdp
lib.rserdp
version0.1.1
sourcesrc
created_at2024-04-12 16:54:08.12595
updated_at2024-05-18 03:45:38.226502
descriptionCrate for display an error and its nested errors
homepage
repositoryhttps://github.com/ultimaweapon/erdp
max_upload_size
id1206508
size6,319
Putta Khunchalee (ultimaweapon)

documentation

README

ERDP

Crates.io Version

ERDP is a very small Rust crate to help with error displaying. If you use std::fmt::Display to display a std::error::Error like the following code:

use std::fs::File;
use std::path::{Path, PathBuf};
use thiserror::Error;

fn main() {
    let path = PathBuf::from("config.json");

    if let Err(e) = load_config(&path) {
        eprintln!("Failed to load {}: {}.", path.display(), e);
    }
}

fn load_config(path: &Path) -> Result<(), MyError> {
    let file = match File::open(path) {
        Ok(v) => v,
        Err(e) => return Err(MyError::OpenFileFailed(e)),
    };

    Ok(())
}

#[derive(Debug, Error)]
enum MyError {
    #[error("couldn't open the specified file")]
    OpenFileFailed(#[source] std::io::Error),
}

What you get is just a message from a top-level error:

Failed to load config.json: couldn't open the specified file.

With this crate you can use display method on the error value like:

use erdp::ErrorDisplay;

eprintln!("Failed to load {}: {}.", path.display(), e.display());

Then the output will change to something like:

Failed to load config.json: couldn't open the specified file -> No such file or directory.

License

MIT

Commit count: 8

cargo fmt