mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-10-02 15:26:08 +00:00
Add panic_note to HookBuilder (#51)
* Implement panic_note feature * Fix typo * Rename to panic_section and use Display impl * Update src/config.rs * Move panic_section after panic message Co-authored-by: Jane Lusby <jlusby42@gmail.com>
This commit is contained in:
parent
a83c478a70
commit
5cf35afaa8
@ -6,7 +6,9 @@ fn main() -> Result<(), Report> {
|
|||||||
#[cfg(feature = "capture-spantrace")]
|
#[cfg(feature = "capture-spantrace")]
|
||||||
install_tracing();
|
install_tracing();
|
||||||
|
|
||||||
color_eyre::install()?;
|
color_eyre::config::HookBuilder::default()
|
||||||
|
.panic_section("consider reporting the bug on github")
|
||||||
|
.install()?;
|
||||||
|
|
||||||
read_config();
|
read_config();
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ use crate::{
|
|||||||
ColorExt,
|
ColorExt,
|
||||||
};
|
};
|
||||||
use ansi_term::Color::*;
|
use ansi_term::Color::*;
|
||||||
|
use fmt::Display;
|
||||||
use indenter::{indented, Format};
|
use indenter::{indented, Format};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
@ -247,6 +248,7 @@ pub struct HookBuilder {
|
|||||||
filters: Vec<Box<FilterCallback>>,
|
filters: Vec<Box<FilterCallback>>,
|
||||||
capture_span_trace_by_default: bool,
|
capture_span_trace_by_default: bool,
|
||||||
display_env_section: bool,
|
display_env_section: bool,
|
||||||
|
panic_section: Option<Box<dyn Display + Send + Sync + 'static>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HookBuilder {
|
impl HookBuilder {
|
||||||
@ -279,9 +281,26 @@ impl HookBuilder {
|
|||||||
filters: vec![],
|
filters: vec![],
|
||||||
capture_span_trace_by_default: false,
|
capture_span_trace_by_default: false,
|
||||||
display_env_section: true,
|
display_env_section: true,
|
||||||
|
panic_section: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a custom section to the panic hook that will be printed
|
||||||
|
/// in the panic message.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// color_eyre::config::HookBuilder::default()
|
||||||
|
/// .panic_section("consider reporting the bug at https://github.com/yaahc/color-eyre")
|
||||||
|
/// .install()
|
||||||
|
/// .unwrap()
|
||||||
|
/// ```
|
||||||
|
pub fn panic_section<S: Display + Send + Sync + 'static>(mut self, section: S) -> Self {
|
||||||
|
self.panic_section = Some(Box::new(section));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Configures the default capture mode for `SpanTraces` in error reports and panics
|
/// Configures the default capture mode for `SpanTraces` in error reports and panics
|
||||||
pub fn capture_span_trace_by_default(mut self, cond: bool) -> Self {
|
pub fn capture_span_trace_by_default(mut self, cond: bool) -> Self {
|
||||||
self.capture_span_trace_by_default = cond;
|
self.capture_span_trace_by_default = cond;
|
||||||
@ -347,6 +366,7 @@ impl HookBuilder {
|
|||||||
pub(crate) fn into_hooks(self) -> (PanicHook, EyreHook) {
|
pub(crate) fn into_hooks(self) -> (PanicHook, EyreHook) {
|
||||||
let panic_hook = PanicHook {
|
let panic_hook = PanicHook {
|
||||||
filters: self.filters.into_iter().map(Into::into).collect(),
|
filters: self.filters.into_iter().map(Into::into).collect(),
|
||||||
|
section: self.panic_section,
|
||||||
#[cfg(feature = "capture-spantrace")]
|
#[cfg(feature = "capture-spantrace")]
|
||||||
capture_span_trace_by_default: self.capture_span_trace_by_default,
|
capture_span_trace_by_default: self.capture_span_trace_by_default,
|
||||||
display_env_section: self.display_env_section,
|
display_env_section: self.display_env_section,
|
||||||
@ -464,6 +484,10 @@ fn print_panic_info(printer: &PanicPrinter<'_>, out: &mut fmt::Formatter<'_>) ->
|
|||||||
|
|
||||||
let mut separated = out.header("\n\n");
|
let mut separated = out.header("\n\n");
|
||||||
|
|
||||||
|
if let Some(ref section) = printer.section {
|
||||||
|
write!(&mut separated.ready(), "{}", section)?;
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "capture-spantrace")]
|
#[cfg(feature = "capture-spantrace")]
|
||||||
{
|
{
|
||||||
if let Some(span_trace) = span_trace.as_ref() {
|
if let Some(span_trace) = span_trace.as_ref() {
|
||||||
@ -502,6 +526,7 @@ fn print_panic_info(printer: &PanicPrinter<'_>, out: &mut fmt::Formatter<'_>) ->
|
|||||||
|
|
||||||
pub(crate) struct PanicHook {
|
pub(crate) struct PanicHook {
|
||||||
filters: Vec<Arc<FilterCallback>>,
|
filters: Vec<Arc<FilterCallback>>,
|
||||||
|
section: Option<Box<dyn Display + Send + Sync + 'static>>,
|
||||||
#[cfg(feature = "capture-spantrace")]
|
#[cfg(feature = "capture-spantrace")]
|
||||||
capture_span_trace_by_default: bool,
|
capture_span_trace_by_default: bool,
|
||||||
display_env_section: bool,
|
display_env_section: bool,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user