Function retry_error::fmt_error_with_sources
source · pub fn fmt_error_with_sources(e: &dyn Error, f: &mut Formatter<'_>) -> Result
Expand description
Helper: formats a std::error::Error
and its sources (as "error: source"
)
Avoids duplication in messages by not printing messages which are wholly-contained (textually) within already-printed messages.
Offered as a fmt
function:
this is for use in more-convenient higher-level error handling functionality,
rather than directly in application/functional code.
This is used by RetryError
’s impl of Display
,
but will be useful for other error-handling situations.
§Example
use std::fmt::{self, Display};
#[derive(Debug, thiserror::Error)]
#[error("some pernickety problem")]
struct Pernickety;
#[derive(Debug, thiserror::Error)]
enum ApplicationError {
#[error("everything is terrible")]
Terrible(#[source] Pernickety),
}
struct Wrapper(Box<dyn std::error::Error>);
impl Display for Wrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
retry_error::fmt_error_with_sources(&*self.0, f)
}
}
let bad = Pernickety;
let err = ApplicationError::Terrible(bad);
let printed = Wrapper(err.into()).to_string();
assert_eq!(printed, "everything is terrible: some pernickety problem");