[−][src]Struct display_error_chain::DisplayErrorChain
Provides an fmt::Display implementation for an error as a chain.
use display_error_chain::DisplayErrorChain; // Let's set up a custom error. Normally one would use `snafu` or // something similar to avoid the boilerplate. #[derive(Debug)] enum CustomError { NoCause, IO { source: std::io::Error }, } // Custom display implementation (which doesn't take error // sources into consideration). impl std::fmt::Display for CustomError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { CustomError::NoCause => { write!(f, "No cause") } CustomError::IO { .. } => { write!(f, "Some I/O") } } } } impl std::error::Error for CustomError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { CustomError::NoCause => None, CustomError::IO { source } => Some(source), } } } // And finally let's see how `DisplayErrorChain` helps! let io = CustomError::IO { source: std::io::Error::new(std::io::ErrorKind::AlreadyExists, "wow"), }; let formatted = DisplayErrorChain::new(&io).to_string(); assert_eq!("Some I/O\nCaused by:\n -> wow", formatted); let no_cause = CustomError::NoCause; let formatted = DisplayErrorChain::new(&no_cause).to_string(); assert_eq!("No cause", formatted);
Implementations
impl<'a, E: ?Sized> DisplayErrorChain<'a, E> where
E: Error, [src]
E: Error,
Trait Implementations
impl<'a, E: ?Sized> Display for DisplayErrorChain<'a, E> where
E: Error, [src]
E: Error,
Auto Trait Implementations
impl<'a, E: ?Sized> RefUnwindSafe for DisplayErrorChain<'a, E> where
E: RefUnwindSafe, [src]
E: RefUnwindSafe,
impl<'a, E: ?Sized> Send for DisplayErrorChain<'a, E> where
E: Sync, [src]
E: Sync,
impl<'a, E: ?Sized> Sync for DisplayErrorChain<'a, E> where
E: Sync, [src]
E: Sync,
impl<'a, E: ?Sized> Unpin for DisplayErrorChain<'a, E>[src]
impl<'a, E: ?Sized> UnwindSafe for DisplayErrorChain<'a, E> where
E: RefUnwindSafe, [src]
E: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized, [src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,