[−][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.
type Report = eyre::Report<MyContext>; // And optionally... type Result<T, E = eyre::Report<MyContext>> = 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 std::backtrace::Backtrace; #[derive(Debug)] pub struct Context { backtrace: Option<Backtrace>, } impl EyreContext for Context { #[allow(unused_variables)] fn default(error: &(dyn Error + 'static)) -> Self { let backtrace = if error.backtrace().is_some() { None } else { Some(Backtrace::capture()) }; 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
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 member_ref(&self, _typeid: TypeId) -> Option<&dyn Any>
Member access function
The main reason to implement this fn is to provide support for eyre::Report::backtrace
which will call this fn on nightly when attempting access the captured
std::backtrace::Backtrace
Example
fn member_ref(&self, typeid: TypeId) -> Option<&dyn Any> { if typeid == TypeId::of::<Backtrace>() { self.backtrace.as_ref().map(|b| b as &dyn Any) } else { None } }