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

pub struct HookBuilder { /* fields omitted */ }
Expand description

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

Implementations

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();

Construct a HookBuilder with minimal features enabled

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.

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()

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"
}

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

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

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();

Install the given Hook as the global error report hook

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

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

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Set the foreground color generically Read more

Set the background color generically. Read more

Change the foreground color to black

Change the foreground color to black

Change the foreground color to red

Change the foreground color to red

Change the foreground color to green

Change the foreground color to green

Change the foreground color to yellow

Change the foreground color to yellow

Change the foreground color to blue

Change the foreground color to blue

Change the foreground color to magenta

Change the foreground color to magenta

Change the foreground color to purple

Change the foreground color to purple

Change the foreground color to cyan

Change the foreground color to cyan

Change the foreground color to white

Change the foreground color to white

Change the foreground color to bright black

Change the foreground color to bright black

Change the foreground color to bright red

Change the foreground color to bright red

Change the foreground color to bright green

Change the foreground color to bright green

Change the foreground color to bright yellow

Change the foreground color to bright yellow

Change the foreground color to bright blue

Change the foreground color to bright blue

Change the foreground color to bright magenta

Change the foreground color to bright magenta

Change the foreground color to bright purple

Change the foreground color to bright purple

Change the foreground color to bright cyan

Change the foreground color to bright cyan

Change the foreground color to bright white

Change the foreground color to bright white

Make the text bold

Make the text dim

Make the text italicized

Make the text italicized

Make the text blink

Make the text blink (but fast!)

Swap the foreground and background colors

Hide the text

Cross out the text

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more

Sets the foreground color to an RGB value.

Sets the background color to an RGB value.

Apply a runtime-determined style

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.