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

Commit 0f85c34

Browse files
committed
Add "source" implementations everywhere
1 parent 94e6540 commit 0f85c34

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/error_chain.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ macro_rules! impl_error_chain_processed {
179179
self.description()
180180
}
181181

182+
#[cfg(not(has_error_source))]
182183
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
183184
fn cause(&self) -> Option<&::std::error::Error> {
184185
match self.1.next_error {
@@ -188,12 +189,26 @@ macro_rules! impl_error_chain_processed {
188189
$(
189190
$(#[$meta_foreign_links])*
190191
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
191-
match () {
192-
#[cfg(not(has_error_source))]
193-
() => foreign_err.cause(),
194-
#[cfg(has_error_source)]
195-
() => foreign_err.source(),
196-
}
192+
foreign_err.cause()
193+
}
194+
) *
195+
_ => None
196+
}
197+
}
198+
}
199+
}
200+
201+
#[cfg(has_error_source)]
202+
#[allow(unknown_lints, renamed_and_removed_lints, unused_doc_comment, unused_doc_comments)]
203+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
204+
match self.1.next_error {
205+
Some(ref c) => Some(&**c),
206+
None => {
207+
match self.0 {
208+
$(
209+
$(#[$meta_foreign_links])*
210+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
211+
foreign_err.source()
197212
}
198213
) *
199214
_ => None

tests/tests.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,15 @@ mod foreign_link_test {
292292
"Foreign error description"
293293
}
294294

295+
#[cfg(not(has_error_source))]
295296
fn cause(&self) -> Option<&::std::error::Error> {
296297
Some(&self.cause)
297298
}
299+
300+
#[cfg(has_error_source)]
301+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
302+
Some(&self.cause)
303+
}
298304
}
299305

300306
impl fmt::Display for ForeignError {
@@ -311,9 +317,15 @@ mod foreign_link_test {
311317
"Foreign error cause description"
312318
}
313319

320+
#[cfg(not(has_error_source))]
314321
fn cause(&self) -> Option<&::std::error::Error> {
315322
None
316323
}
324+
325+
#[cfg(has_error_source)]
326+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
327+
None
328+
}
317329
}
318330

319331
impl fmt::Display for ForeignErrorCause {
@@ -353,7 +365,8 @@ mod foreign_link_test {
353365
#[cfg(has_error_source)]
354366
fn finds_source() {
355367
let chained_error = try_foreign_error().err().unwrap();
356-
assert!(::std::error::Error::source(&chained_error).is_none());
368+
assert_eq!(format!("{}", ForeignErrorCause {}),
369+
format!("{}", ::std::error::Error::source(&chained_error).unwrap()));
357370
}
358371

359372
#[test]
@@ -363,13 +376,8 @@ mod foreign_link_test {
363376
assert!(!format!("{:?}", error_iter).is_empty());
364377
assert_eq!(format!("{}", ForeignError { cause: ForeignErrorCause {} }),
365378
format!("{}", error_iter.next().unwrap()));
366-
match () {
367-
#[cfg(not(has_error_source))]
368-
() => assert_eq!(format!("{}", ForeignErrorCause {}),
369-
format!("{}", error_iter.next().unwrap())),
370-
#[cfg(has_error_source)]
371-
() => assert!(error_iter.next().is_none()),
372-
};
379+
assert_eq!(format!("{}", ForeignErrorCause {}),
380+
format!("{}", error_iter.next().unwrap()));
373381
assert_eq!(format!("{:?}", None as Option<&::std::error::Error>),
374382
format!("{:?}", error_iter.next()));
375383
}

0 commit comments

Comments
 (0)