Skip to content
Myr

Move error formatting to fmt.rs

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent b1c7c7a · Browse files · 3 files+70 added−65 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+70 added−65 removed

  • other:+70 added−65 removed

3 files+70 added−65 removed

modified:src/error.rs+4 added-65 removed
650 unmodified lines
651 }652}653654impl ErrorImpl<()> {655 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {655 pub(crate) fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {656 unsafe { &*(self.vtable.object_ref)(self) }657 }658659 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {659 pub(crate) fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {660 unsafe { &mut *(self.vtable.object_mut)(self) }661 }662663 #[cfg(backtrace)]664 fn backtrace(&self) -> &Backtrace {664 pub(crate) fn backtrace(&self) -> &Backtrace {665 // This unwrap can only panic if the underlying error's backtrace method666 // is nondeterministic, which would only happen in maliciously667 // constructed code.668 self.backtrace
1 unmodified line
670 .or_else(|| self.error().backtrace())671 .expect("backtrace capture failed")672 }673674 fn chain(&self) -> Chain {674 pub(crate) fn chain(&self) -> Chain {675 Chain {676 next: Some(self.error()),677 }678 }679680 fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result {681 write!(f, "{}", self.error())?;682683 let mut chain = self.chain().skip(1).enumerate().peekable();684 if let Some((n, error)) = chain.next() {685 write!(f, "\n\nCaused by:\n ")?;686 if chain.peek().is_some() {687 write!(f, "{}: ", n)?;688 }689 write!(f, "{}", error)?;690 for (n, error) in chain {691 write!(f, "\n {}: {}", n, error)?;692 }693 }694695 Ok(())696 }697698 fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {699 if f.alternate() {700 self.print_chain(f)701 } else {702 write!(f, "{}", self.error())703 }704 }705706 fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {707 if f.alternate() {708 return Debug::fmt(self.error(), f);709 }710711 self.print_chain(f)?;712 writeln!(f)?;713714 #[cfg(backtrace)]715 {716 use std::backtrace::BacktraceStatus;717718 let backtrace = self.backtrace();719 match backtrace.status() {720 BacktraceStatus::Captured => {721 let mut backtrace = backtrace.to_string();722 if backtrace.starts_with("stack backtrace:") {723 // Capitalize to match "Caused by:"724 backtrace.replace_range(0..1, "S");725 }726 write!(f, "\n{}", backtrace)?;727 }728 BacktraceStatus::Disabled => {729 writeln!(730 f,731 "\nStack backtrace:\n Run with RUST_LIB_BACKTRACE=1 env variable to display a backtrace"732 )?;733 }734 _ => {}735 }736 }737738 Ok(())739 }679}680681impl<E> StdError for ErrorImpl<E>682where
56 unmodified lines
added:src/fmt.rs+65 added
1use crate::error::ErrorImpl;2use std::fmt::{self, Debug};34impl ErrorImpl<()> {5 fn print_chain(&self, f: &mut fmt::Formatter) -> fmt::Result {6 write!(f, "{}", self.error())?;78 let mut chain = self.chain().skip(1).enumerate().peekable();9 if let Some((n, error)) = chain.next() {10 write!(f, "\n\nCaused by:\n ")?;11 if chain.peek().is_some() {12 write!(f, "{}: ", n)?;13 }14 write!(f, "{}", error)?;15 for (n, error) in chain {16 write!(f, "\n {}: {}", n, error)?;17 }18 }1920 Ok(())21 }2223 pub(crate) fn display(&self, f: &mut fmt::Formatter) -> fmt::Result {24 if f.alternate() {25 self.print_chain(f)26 } else {27 write!(f, "{}", self.error())28 }29 }3031 pub(crate) fn debug(&self, f: &mut fmt::Formatter) -> fmt::Result {32 if f.alternate() {33 return Debug::fmt(self.error(), f);34 }3536 self.print_chain(f)?;37 writeln!(f)?;3839 #[cfg(backtrace)]40 {41 use std::backtrace::BacktraceStatus;4243 let backtrace = self.backtrace();44 match backtrace.status() {45 BacktraceStatus::Captured => {46 let mut backtrace = backtrace.to_string();47 if backtrace.starts_with("stack backtrace:") {48 // Capitalize to match "Caused by:"49 backtrace.replace_range(0..1, "S");50 }51 write!(f, "\n{}", backtrace)?;52 }53 BacktraceStatus::Disabled => {54 writeln!(55 f,56 "\nStack backtrace:\n Run with RUST_LIB_BACKTRACE=1 env variable to display a backtrace"57 )?;58 }59 _ => {}60 }61 }6263 Ok(())64 }65}
modified:src/lib.rs+1 added
169 unmodified lines
170#[macro_use]171mod backtrace;172mod context;173mod error;174mod fmt;175mod kind;176mod macros;177178#[cfg(not(feature = "std"))]
271 unmodified lines