Add full example to readme

This commit is contained in:
Jane Lusby 2020-04-25 10:08:45 -07:00
parent af67c25f9f
commit 043abca81c

View File

@ -52,6 +52,46 @@ fn example(&self) -> color_eyre::Result<()> {
}
```
# Example
```should_panic
use color_eyre::{Help, Report};
use eyre::WrapErr;
use tracing::{info, instrument};
use tracing_error::ErrorLayer;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};
#[instrument]
fn main() -> Result<(), Report> {
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("info"))
.unwrap();
tracing_subscriber::registry()
.with(filter_layer)
.with(fmt_layer)
.with(ErrorLayer::default())
.init();
Ok(read_config()?)
}
#[instrument]
fn read_file(path: &str) -> Result<(), Report> {
info!("Reading file");
Ok(std::fs::read_to_string(path).map(drop)?)
}
#[instrument]
fn read_config() -> Result<(), Report> {
read_file("fake_file")
.wrap_err("Unable to read config")
.suggestion("try using a file that exists next time")
}
```
## Minimal Report Format
![minimal report format](./pictures/minimal.png)