| use crate::chain::Chain; |
| use crate::error::ErrorImpl; |
| use core::fmt::{self, Debug}; |
| |
| impl ErrorImpl<()> { |
| pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| write!(f, "{}", self.error())?; |
| |
| if f.alternate() { |
| for cause in self.chain().skip(1) { |
| write!(f, ": {}", cause)?; |
| } |
| } |
| |
| Ok(()) |
| } |
| |
| pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| let error = self.error(); |
| |
| if f.alternate() { |
| return 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() { |
| write!(f, "\n ")?; |
| if multiple { |
| write!(f, "{}: ", n)?; |
| } |
| write!(f, "{}", error)?; |
| } |
| } |
| |
| #[cfg(backtrace)] |
| { |
| use std::backtrace::BacktraceStatus; |
| |
| let backtrace = self.backtrace(); |
| if let BacktraceStatus::Captured = backtrace.status() { |
| let mut backtrace = backtrace.to_string(); |
| if backtrace.starts_with("stack backtrace:") { |
| // Capitalize to match "Caused by:" |
| backtrace.replace_range(0..1, "S"); |
| } |
| backtrace.truncate(backtrace.trim_end().len()); |
| write!(f, "\n\n{}", backtrace)?; |
| } |
| } |
| |
| Ok(()) |
| } |
| } |