Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

Added support for rendering errors with causes #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ fn bad_function() -> Result<(), WrappingError> {
}

fn main() {
for cause in Fail::iter_causes(&bad_function().unwrap_err()) {
println!("{}", cause);
}
println!("{}", Fail::display(&bad_function().unwrap_err()));
}
5 changes: 5 additions & 0 deletions src/backtrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ with_backtrace! {
Backtrace { internal: InternalBacktrace::new() }
}

/// Checks if the backtrace is empty.
pub fn is_empty(&self) -> bool {
self.internal.is_none()
}

pub(crate) fn none() -> Backtrace {
Backtrace { internal: InternalBacktrace::none() }
}
Expand Down
43 changes: 43 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use _core::fmt;

use Fail;
use backtrace::Backtrace;


/// Renders a fail with all causes.
pub struct FailDisplay<'a>(pub(crate) &'a Fail, pub(crate) Option<&'a Backtrace>);

impl<'a> fmt::Display for FailDisplay<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut ptr = Some(self.0);
let mut idx = 0;
let mut was_backtrace = false;

while let Some(fail) = ptr {
if was_backtrace {
write!(f, "\n")?;
}
was_backtrace = false;
if idx == 0 {
write!(f, "error: {}", fail)?;
} else {
write!(f, "\n caused by: {}", fail)?;
}
if f.alternate() {
let backtrace = if idx == 0 && self.1.is_some() {
Some(self.1.unwrap())
} else {
fail.backtrace()
};
if let Some(backtrace) = backtrace {
write!(f, "\nbacktrace:\n{}", backtrace)?;
was_backtrace = true;
}
}
ptr = fail.cause();
idx += 1;
}

Ok(())
}
}
7 changes: 7 additions & 0 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use self::error_impl::ErrorImpl;
#[cfg(feature = "std")]
use std::error::Error as StdError;

use display::FailDisplay;


/// The `Error` type, which can contain any failure.
///
Expand Down Expand Up @@ -155,6 +157,11 @@ impl Error {
self.imp.failure_mut().downcast_mut()
}

/// Displays the error.
pub fn display(&self) -> FailDisplay {
FailDisplay(self.as_fail(), Some(self.backtrace()))
}

/// Deprecated alias to `find_root_cause`.
#[deprecated(since = "0.1.2", note = "please use the 'find_root_cause()' method instead")]
pub fn root_cause(&self) -> &Fail {
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod box_std;
mod compat;
mod context;
mod result_ext;
mod display;

use core::any::TypeId;
use core::fmt::{Debug, Display};
Expand All @@ -47,6 +48,7 @@ pub use backtrace::Backtrace;
pub use compat::Compat;
pub use context::Context;
pub use result_ext::ResultExt;
pub use display::FailDisplay;

#[cfg(feature = "failure_derive")]
#[allow(unused_imports)]
Expand Down Expand Up @@ -230,6 +232,11 @@ impl Fail {
Causes { fail: Some(self) }
}

/// Displays the failure.
pub fn display(&self) -> FailDisplay {
FailDisplay(self, None)
}

/// Deprecated alias to `find_root_cause`.
#[deprecated(since = "0.1.2", note = "please use the 'find_root_cause()' method instead")]
pub fn root_cause(&self) -> &Fail {
Expand Down