Skip to content

Commit

Permalink
fix(InnermostErrorMessage): do not infinite loop here
Browse files Browse the repository at this point in the history
  • Loading branch information
coriolinus committed Dec 17, 2024
1 parent a05e6a6 commit f8a756b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crypto/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,22 @@ pub trait InnermostErrorMessage {
impl<E: std::error::Error> 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());
}
}

0 comments on commit f8a756b

Please sign in to comment.