Struct color_eyre::config::HookBuilder[][src]

pub struct HookBuilder { /* fields omitted */ }

Builder for customizing the behavior of the global panic and error report hooks

Implementations

impl HookBuilder[src]

pub fn new() -> Self[src]

Construct a HookBuilder

Details

By default this function calls add_default_filters() and capture_span_trace_by_default(true). To get a HookBuilder with all features disabled by default call HookBuilder::blank().

Example

use color_eyre::config::HookBuilder;

HookBuilder::new()
    .install()
    .unwrap();

pub fn blank() -> Self[src]

Construct a HookBuilder with minimal features enabled

pub fn theme(self, theme: Theme) -> Self[src]

Set the global styles that color_eyre should use.

Tip: You can test new styles by editing examples/theme.rs in the color-eyre repository.

pub fn panic_section<S: Display + Send + Sync + 'static>(
    self,
    section: S
) -> Self
[src]

Add a custom section to the panic hook that will be printed in the panic message.

Examples

color_eyre::config::HookBuilder::default()
    .panic_section("consider reporting the bug at https://github.com/yaahc/color-eyre")
    .install()
    .unwrap()

pub fn panic_message<S: PanicMessage>(self, section: S) -> Self[src]

Overrides the main error message printing section at the start of panic reports

Examples

use std::{panic::Location, fmt};
use color_eyre::section::PanicMessage;
use owo_colors::OwoColorize;

struct MyPanicMessage;

color_eyre::config::HookBuilder::default()
    .panic_message(MyPanicMessage)
    .install()
    .unwrap();

impl PanicMessage for MyPanicMessage {
    fn display(&self, pi: &std::panic::PanicInfo<'_>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "{}", "The application panicked (crashed).".red())?;

        // Print panic message.
        let payload = pi
            .payload()
            .downcast_ref::<String>()
            .map(String::as_str)
            .or_else(|| pi.payload().downcast_ref::<&str>().cloned())
            .unwrap_or("<non string panic payload>");

        write!(f, "Message:  ")?;
        writeln!(f, "{}", payload.cyan())?;

        // If known, print panic location.
        write!(f, "Location: ")?;
        if let Some(loc) = pi.location() {
            write!(f, "{}", loc.file().purple())?;
            write!(f, ":")?;
            write!(f, "{}", loc.line().purple())?;

            write!(f, "\n\nConsider reporting the bug at {}", custom_url(loc, payload))?;
        } else {
            write!(f, "<unknown>")?;
        }

        Ok(())
    }
}

fn custom_url(location: &Location<'_>, message: &str) -> impl fmt::Display {
    "todo"
}

pub fn issue_url<S: ToString>(self, url: S) -> Self[src]

This is supported on crate feature issue-url only.

Set an upstream github repo and enable issue reporting url generation

Details

Once enabled, color-eyre will generate urls that will create customized issues pre-populated with information about the associated error report.

Additional information can be added to the metadata table in the generated urls by calling add_issue_metadata when configuring the HookBuilder.

Examples

color_eyre::config::HookBuilder::default()
    .issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new"))
    .install()
    .unwrap();

pub fn add_issue_metadata<K, V>(self, key: K, value: V) -> Self where
    K: Display,
    V: Display + Send + Sync + 'static, 
[src]

This is supported on crate feature issue-url only.

Add a new entry to the metadata table in generated github issue urls

Note: this metadata will be ignored if no issue_url is set.

Examples

color_eyre::config::HookBuilder::default()
    .issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new"))
    .add_issue_metadata("version", env!("CARGO_PKG_VERSION"))
    .install()
    .unwrap();

pub fn issue_filter<F>(self, predicate: F) -> Self where
    F: Fn(ErrorKind<'_>) -> bool + Send + Sync + 'static, 
[src]

This is supported on crate feature issue-url only.

Configures a filter for disabling issue url generation for certain kinds of errors

If the closure returns true, then the issue url will be generated.

Examples

color_eyre::config::HookBuilder::default()
    .issue_url(concat!(env!("CARGO_PKG_REPOSITORY"), "/issues/new"))
    .issue_filter(|kind| match kind {
        color_eyre::ErrorKind::NonRecoverable(payload) => {
            let payload = payload
                .downcast_ref::<String>()
                .map(String::as_str)
                .or_else(|| payload.downcast_ref::<&str>().cloned())
                .unwrap_or("<non string panic payload>");

            !payload.contains("my irrelevant error message")
        },
        color_eyre::ErrorKind::Recoverable(error) => !error.is::<std::fmt::Error>(),
    })
    .install()
    .unwrap();

pub fn capture_span_trace_by_default(self, cond: bool) -> Self[src]

Configures the default capture mode for SpanTraces in error reports and panics

pub fn display_env_section(self, cond: bool) -> Self[src]

Configures the enviroment varible info section and whether or not it is displayed

pub fn add_frame_filter(self, filter: Box<FilterCallback>) -> Self[src]

Add a custom filter to the set of frame filters

Examples

color_eyre::config::HookBuilder::default()
    .add_frame_filter(Box::new(|frames| {
        let filters = &[
            "uninteresting_function",
        ];

        frames.retain(|frame| {
            !filters.iter().any(|f| {
                let name = if let Some(name) = frame.name.as_ref() {
                    name.as_str()
                } else {
                    return true;
                };

                name.starts_with(f)
            })
        });
    }))
    .install()
    .unwrap();

pub fn install(self) -> Result<(), Report>[src]

Install the given Hook as the global error report hook

pub fn add_default_filters(self) -> Self[src]

Add the default set of filters to this HookBuilder’s configuration

pub fn into_hooks(self) -> (PanicHook, EyreHook)[src]

Create a PanicHook and EyreHook from this HookBuilder. This can be used if you want to combine these handlers with other handlers.

Trait Implementations

impl Default for HookBuilder[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<D> OwoColorize for D[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.