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

Commit

Permalink
Remove the Copy requirement for foreign errors and show the error cau…
Browse files Browse the repository at this point in the history
…se in Display
  • Loading branch information
cramertj committed Jul 27, 2016
1 parent 9e5bc81 commit a1f69e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ macro_rules! error_chain {

impl ::std::fmt::Display for $error_name {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&self.0, f)
let display_string = if let Some(ref cause) = (self.1).0 {
format!("{}: {}", &self.0, cause)
} else {
format!("{}", &self.0)
};
::std::fmt::Display::fmt(&display_string, f)
}
}

Expand All @@ -467,7 +472,7 @@ macro_rules! error_chain {
impl From<$foreign_link_error_path> for $error_name {
fn from(e: $foreign_link_error_path) -> Self {
$error_name(
$error_kind_name::$foreign_link_variant(e),
$error_kind_name::$foreign_link_variant,
(Some(Box::new(e)),
::std::sync::Arc::new($crate::Backtrace::new())))
}
Expand Down Expand Up @@ -516,9 +521,8 @@ macro_rules! error_chain {
) *

$(
$foreign_link_variant(err: $foreign_link_error_path) {
$foreign_link_variant {
description(&$foreign_link_desc)
display("{}", err)
}
) *

Expand Down
12 changes: 7 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn smoke_test_1() {

foreign_links { }

errors { }
errors { }
}
}

Expand All @@ -27,7 +27,7 @@ fn smoke_test_2() {

foreign_links { }

errors { }
errors { }
}
}

Expand All @@ -38,7 +38,7 @@ fn smoke_test_3() {

foreign_links { }

errors { }
errors { }
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ mod foreign_link_test {

// Note: foreign errors must be `pub` because they appear in the
// signature of the public foreign_link_error_path
#[derive(Copy, Clone, Debug)]
#[derive(Debug)]
pub struct ForeignError {}

impl StdError for ForeignError {
Expand All @@ -94,6 +94,8 @@ mod foreign_link_test {

fn cause(&self) -> Option<&StdError> { None }
}

// StdError requires Display so we can depend on error types being Display
impl fmt::Display for ForeignError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Foreign error display")
Expand All @@ -115,7 +117,7 @@ mod foreign_link_test {
fn display_underlying_error() {
let chained_error = try_foreign_error().err().unwrap();
assert_eq!(
format!("{}", ForeignError{}),
format!("{}: {}", "some foreign error", ForeignError{}),
format!("{}", chained_error)
);
}
Expand Down

0 comments on commit a1f69e7

Please sign in to comment.