From f8a756bfd6578866fdbdf6e47de92f15c463526c Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Tue, 17 Dec 2024 15:21:15 +0100 Subject: [PATCH] fix(InnermostErrorMessage): do not infinite loop here --- crypto/src/error/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crypto/src/error/mod.rs b/crypto/src/error/mod.rs index 7e84d3c27f..c5cbd671b5 100644 --- a/crypto/src/error/mod.rs +++ b/crypto/src/error/mod.rs @@ -79,9 +79,22 @@ pub trait InnermostErrorMessage { impl InnermostErrorMessage for E { fn innermost_error_message(&self) -> String { let mut err: &dyn std::error::Error = self; - while let Some(source) = self.source() { + while let Some(source) = err.source() { err = source; } err.to_string() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_unpack_wrapped_error() { + let inner = Error::InvalidContext; + let outer = RecursiveError::root("wrapping the inner for test purposes")(inner); + let message = outer.innermost_error_message(); + assert_eq!(message, Error::InvalidContext.to_string()); + } +}