[][src]Trait eyre::EyreHandler

pub trait EyreHandler: Sized + Send + Sync + 'static {
    fn default(err: &(dyn StdError + 'static)) -> Self;
fn debug(
        &self,
        error: &(dyn StdError + 'static),
        f: &mut Formatter
    ) -> Result; fn display(
        &self,
        error: &(dyn StdError + 'static),
        f: &mut Formatter
    ) -> Result { ... } }

Error Report Handler trait for customizing eyre::Report

Customization

In order to insert your own custom context and report format you must first implement the eyre::EyreHandler trait.

Once you've defined a custom Handler type you can use it throughout your application by defining a type alias.

use backtrace::Backtrace;
use eyre::EyreHandler;
use indenter::indented;

pub struct Handler {
    backtrace: Backtrace,
}

impl EyreHandler for Handler {
    // ...
}

type Report = eyre::Report<Handler>;
type Result<T, E = eyre::Report<Handler>> = core::result::Result<T, E>;

Required methods

fn default(err: &(dyn StdError + 'static)) -> Self

Default construct a Handler when constructing a Report.

This method provides a reference to the error being wrapped to support conditional capturing of context like backtrace depending on whether the source error already captured one.

Example

use backtrace::Backtrace;
use eyre::EyreHandler;
use std::error::Error;

pub struct Handler {
    backtrace: Backtrace,
}

impl EyreHandler for Handler {
    fn default(error: &(dyn Error + 'static)) -> Self {
        let backtrace = Backtrace::new();

        Self { backtrace }
    }

    // ...
}

fn debug(&self, error: &(dyn StdError + 'static), f: &mut Formatter) -> Result

Define the report format

Used to override the report format of eyre::Report

Example

use backtrace::Backtrace;
use eyre::EyreHandler;
use eyre::Chain;
use std::error::Error;
use indenter::indented;

pub struct Handler {
    backtrace: Backtrace,
}

impl EyreHandler for Handler {
    // ...

    fn debug(
        &self,
        error: &(dyn Error + 'static),
        f: &mut core::fmt::Formatter<'_>,
    ) -> core::fmt::Result {
        use core::fmt::Write as _;

        if f.alternate() {
            return core::fmt::Debug::fmt(error, f);
        }

        write!(f, "{}", error)?;

        if let Some(cause) = error.source() {
            write!(f, "\n\nCaused by:")?;
            let multiple = cause.source().is_some();

            for (n, error) in Chain::new(cause).enumerate() {
                writeln!(f)?;
                if multiple {
                    write!(indented(f).ind(n), "{}", error)?;
                } else {
                    write!(indented(f), "{}", error)?;
                }
            }
        }

        let backtrace = &self.backtrace;
        write!(f, "\n\nStack backtrace:\n{:?}", backtrace)?;

        Ok(())
    }
}
Loading content...

Provided methods

fn display(&self, error: &(dyn StdError + 'static), f: &mut Formatter) -> Result

Override for the Display format

Loading content...

Implementors

impl EyreHandler for DefaultHandler[src]

Loading content...