mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-28 05:21:34 +00:00
pre-release version
This commit is contained in:
parent
cb9fcb85c3
commit
373e6d8a1e
@ -12,6 +12,12 @@ tracing-error = "0.1.2"
|
||||
color-backtrace = "0.3.0"
|
||||
backtrace = "0.3"
|
||||
indenter = "0.1.3"
|
||||
console = "0.10.0"
|
||||
color-spantrace = { git = "https://github.com/yaahc/color-spantrace.git" }
|
||||
|
||||
[patch.crates-io]
|
||||
color-backtrace = { path = "/home/jlusby/git/rust/color-backtrace" }
|
||||
color-backtrace = { git = "https://github.com/yaahc/color-backtrace.git", branch = "filter-custom" }
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-subscriber = "0.2.5"
|
||||
tracing = "0.1.13"
|
||||
|
@ -1,6 +1,36 @@
|
||||
use human_eyre::{eyre, Report};
|
||||
use human_eyre::{Help, Report, WrapErr};
|
||||
use tracing::{info, instrument};
|
||||
use tracing_error::ErrorLayer;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::{fmt, EnvFilter};
|
||||
|
||||
#[instrument]
|
||||
fn main() {
|
||||
let e: Report = eyre!("some random issue");
|
||||
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();
|
||||
|
||||
let e = read_config().unwrap_err();
|
||||
|
||||
println!("Error: {:?}", e);
|
||||
}
|
||||
|
||||
#[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")
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
use crate::{Report, Result};
|
||||
use console::style;
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
/// A helper trait for attaching help text to errors to be displayed after the chain of errors
|
||||
@ -185,7 +186,7 @@ impl Display for HelpInfo {
|
||||
match self {
|
||||
Self::Note(context) => write!(f, "Note: {}", context),
|
||||
Self::Warning(context) => write!(f, "Warning: {}", context),
|
||||
Self::Suggestion(context) => write!(f, "Suggestion: {}", context),
|
||||
Self::Suggestion(context) => write!(f, "{}: {}", style("Suggestion").cyan(), context),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
45
src/lib.rs
45
src/lib.rs
@ -3,19 +3,19 @@ mod help;
|
||||
pub use eyre::*;
|
||||
|
||||
pub use help::Help;
|
||||
pub type Report = eyre::Report<JaneContext>;
|
||||
pub type Report = eyre::Report<Context>;
|
||||
pub type Result<T, E = Report> = core::result::Result<T, E>;
|
||||
|
||||
use backtrace::Backtrace;
|
||||
use console::style;
|
||||
use help::HelpInfo;
|
||||
use indenter::Indented;
|
||||
use std::any::{Any, TypeId};
|
||||
use std::error::Error;
|
||||
use std::fmt::Write as _;
|
||||
use tracing_error::{ExtractSpanTrace, SpanTrace, SpanTraceStatus};
|
||||
|
||||
pub struct JaneContext {
|
||||
backtrace: Backtrace,
|
||||
pub struct Context {
|
||||
backtrace: Option<Backtrace>,
|
||||
span_trace: Option<SpanTrace>,
|
||||
help: Vec<HelpInfo>,
|
||||
}
|
||||
@ -27,9 +27,9 @@ fn get_deepest_spantrace<'a>(error: &'a (dyn Error + 'static)) -> Option<&'a Spa
|
||||
.next()
|
||||
}
|
||||
|
||||
impl EyreContext for JaneContext {
|
||||
impl EyreContext for Context {
|
||||
fn default(error: &(dyn std::error::Error + 'static)) -> Self {
|
||||
let backtrace = Backtrace::new();
|
||||
let backtrace = if true { Some(Backtrace::new()) } else { None };
|
||||
|
||||
let span_trace = if get_deepest_spantrace(error).is_none() {
|
||||
Some(SpanTrace::capture())
|
||||
@ -54,17 +54,12 @@ impl EyreContext for JaneContext {
|
||||
}
|
||||
|
||||
let errors = Chain::new(error)
|
||||
.rev()
|
||||
.filter(|e| e.span_trace().is_none())
|
||||
.enumerate();
|
||||
|
||||
for (n, error) in errors {
|
||||
writeln!(f)?;
|
||||
write!(Indented::numbered(f, n), "{}", error)?;
|
||||
}
|
||||
|
||||
for help in &self.help {
|
||||
write!(f, "\n{}", help)?;
|
||||
write!(Indented::numbered(f, n), "{}", style(error).red().dim())?;
|
||||
}
|
||||
|
||||
let span_trace = self
|
||||
@ -74,20 +69,28 @@ impl EyreContext for JaneContext {
|
||||
.expect("SpanTrace capture failed");
|
||||
|
||||
match span_trace.status() {
|
||||
SpanTraceStatus::CAPTURED => write!(f, "\n\nSpan Trace:\n{}", span_trace)?,
|
||||
SpanTraceStatus::CAPTURED => write!(f, "\n\n{}", color_spantrace::colorize(span_trace))?,
|
||||
SpanTraceStatus::UNSUPPORTED => write!(f, "\n\nWarning: SpanTrace capture is Unsupported.\nEnsure that you've setup an error layer and the versions match")?,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
let backtrace = &self.backtrace;
|
||||
if let Some(backtrace) = self.backtrace.as_ref() {
|
||||
write!(f, "\n\n")?;
|
||||
let settings = color_backtrace::Settings::default().add_post_panic_frames(&[
|
||||
"<human_eyre::Context as eyre::EyreContext>::default",
|
||||
"eyre::",
|
||||
]);
|
||||
|
||||
write!(f, "\n\n")?;
|
||||
let settings = color_backtrace::Settings::default();
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
color_backtrace::print_backtrace(&backtrace, &settings)
|
||||
)?;
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
color_backtrace::print_backtrace(&backtrace, &settings)
|
||||
)?;
|
||||
}
|
||||
|
||||
for help in &self.help {
|
||||
write!(f, "\n{}", help)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user