pub fn downcast_source<'a, T>(error: &'a (dyn std::error::Error + 'static)) -> Option<&'a T>
where
T: std::error::Error + 'static,
{
const MAX_HOPS: usize = 16;
let mut source: Option<&(dyn std::error::Error + 'static)> = Some(error);
for _ in 0..MAX_HOPS {
let Some(e) = source else {
break;
};
if let Some(t) = e.downcast_ref::<T>() {
return Some(t);
}
source = e.source();
}
None
}
pub const DETAILS_SEPARATOR: &str = "\nDetails:";
pub fn format(error: impl AsRef<dyn std::error::Error>) -> String {
format_ref(error.as_ref())
}
pub fn format_ref(error: &dyn std::error::Error) -> String {
let mut string = error.to_string();
for source in std::iter::successors(error.source(), |error| error.source()) {
string.push_str(": ");
string.push_str(&source.to_string());
}
string
}
pub fn format_with_details(error: impl Into<String>, details: impl Into<String>) -> String {
let error = error.into();
let details = details.into();
if details.is_empty() {
error
} else {
format!("{error}{DETAILS_SEPARATOR} {details}")
}
}
pub fn split_details(message: &str) -> (&str, Option<&str>) {
if let Some((summary, details)) = message.split_once(DETAILS_SEPARATOR) {
let details = details.trim();
if details.is_empty() {
(summary, None)
} else {
(summary, Some(details))
}
} else {
(message, None)
}
}
#[test]
fn test_format() {
let err = anyhow::format_err!("root_cause")
.context("inner_context")
.context("outer_context");
assert_eq!(err.to_string(), "outer_context");
assert_eq!(format(&err), "outer_context: inner_context: root_cause");
}
#[test]
fn test_format_with_details() {
assert_eq!(
format_with_details("Error", "The fine print"),
"Error\nDetails: The fine print"
);
}
#[test]
fn test_downcast_source() {
#[derive(Debug)]
struct Leaf(&'static str);
impl std::fmt::Display for Leaf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0)
}
}
impl std::error::Error for Leaf {}
#[derive(Debug)]
struct Wrap(Box<dyn std::error::Error + Send + Sync + 'static>);
impl std::fmt::Display for Wrap {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "wrap: {}", self.0)
}
}
impl std::error::Error for Wrap {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.0.as_ref())
}
}
let wrapped = Wrap(Box::new(Leaf("boom")));
let found = downcast_source::<Leaf>(&wrapped).expect("Leaf should be recoverable");
assert_eq!(found.0, "boom");
let direct = Leaf("direct");
assert!(downcast_source::<Leaf>(&direct).is_some());
let only_wrap = Wrap(Box::new(Leaf("inner")));
assert!(downcast_source::<std::io::Error>(&only_wrap).is_none());
}
#[test]
fn test_split_details() {
for (in_summary, in_details) in [("just a message", ""), ("message", "the fine print")] {
let combined = format_with_details(in_summary, in_details);
let (out_summary, out_details) = split_details(&combined);
assert_eq!(out_summary, in_summary);
assert_eq!(
out_details,
if in_details.is_empty() {
None
} else {
Some(in_details)
}
);
}
}