[−][src]Trait eyre::EyreContext
Context trait for customizing eyre::Report
Customization
In order to insert your own custom context type you must first implement the
eyre::EyreContext
trait.
Once you've defined a custom Context type you can use it throughout your application by defining a type alias.
use backtrace::Backtrace; use eyre::EyreContext; pub struct Context { backtrace: Backtrace, } impl EyreContext for Context { // ... } type Report = eyre::Report<Context>; type Result<T, E = eyre::Report<Context>> = core::result::Result<T, E>;
Required methods
fn default(err: &(dyn StdError + 'static)) -> Self
Default construct a Context
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::EyreContext; use std::error::Error; pub struct Context { backtrace: Backtrace, } impl EyreContext for Context { 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::EyreContext; use eyre::Chain; use std::error::Error; pub struct Context { backtrace: Backtrace, } impl EyreContext for Context { // ... 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!(indenter::Indented::numbered(f, n), "{}", error)?; } else { write!(indenter::Indented::new(f), "{}", error)?; } } } let backtrace = &self.backtrace; write!(f, "\n\nStack backtrace:\n{:?}", backtrace)?; Ok(()) } }
Provided methods
fn display(&self, error: &(dyn StdError + 'static), f: &mut Formatter) -> Result
Override for the Display
format