pub struct HookBuilder { /* private fields */ }
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"
}
Available 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();
Available 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();
Available 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();

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

Available on crate feature track-caller only.

Configures the location info section and whether or not it is displayed.

Notes

This will not disable the location section in a panic message.

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

Returns the argument unchanged.

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

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Set the foreground color generically Read more

Set the background color generically. Read more

Change the foreground color to black

Change the background color to black

Change the foreground color to red

Change the background color to red

Change the foreground color to green

Change the background color to green

Change the foreground color to yellow

Change the background color to yellow

Change the foreground color to blue

Change the background color to blue

Change the foreground color to magenta

Change the background color to magenta

Change the foreground color to purple

Change the background color to purple

Change the foreground color to cyan

Change the background color to cyan

Change the foreground color to white

Change the background color to white

Change the foreground color to the terminal default

Change the background color to the terminal default

Change the foreground color to bright black

Change the background color to bright black

Change the foreground color to bright red

Change the background color to bright red

Change the foreground color to bright green

Change the background color to bright green

Change the foreground color to bright yellow

Change the background color to bright yellow

Change the foreground color to bright blue

Change the background color to bright blue

Change the foreground color to bright magenta

Change the background color to bright magenta

Change the foreground color to bright purple

Change the background color to bright purple

Change the foreground color to bright cyan

Change the background color to bright cyan

Change the foreground color to bright white

Change the background 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

Set the foreground color to a specific RGB value.

Set the background color to a specific RGB value.

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.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more